Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MediaPlayer - how to play in the STREAM_ALARM?

I've tried settings the audio stream of the media player in my application using the following code but when I do this I hear no sound in the emulator. If I don't set the stream for the player then the audio plays fine. I'm sure I'm using this wrong but cannot workout how, any help?

MediaPlayer player = MediaPlayer.create(getApplicationContext(), R.raw.test_audio);

AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.start();

Note: I've added the MODIFY_AUDIO_SETTINGS permission to my manifest already.

Thanks!

like image 964
James Avatar asked Aug 14 '11 13:08

James


People also ask

How can I create media player in Android?

Android Audio Player Example As discussed create a new raw folder in res directory and add one music file like as shown below to play it by using MediaPlayer class. Now open activity_main. xml file from \res\layout folder path and write the code like as shown below.

What is the use of media player in Android?

You can play audio or video from media files stored in your application's resources (raw resources), from standalone files in the filesystem, or from a data stream arriving over a network connection, all using MediaPlayer APIs. Note: You can play back the audio data only to the standard output device.

What is audio manager in android?

AudioManager provides access to volume and ringer mode control.


2 Answers

I don't know why this would happen, however the code below works. You should set the data source with setDataSource() instead of with create().

This code works:

MediaPlayer mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.setDataSource(this,Uri.parse("android.resource://PACKAGE_NAME/"+R.raw.soundfile));
mp.prepare();
mp.start();

This code doesn't work:

MediaPlayer mp = MediaPlayer.create(this, R.raw.soundfile);
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.prepare();
mp.start();
like image 181
Yuki Awano Avatar answered Sep 21 '22 18:09

Yuki Awano


The issue is you are using MediaPlayer.create() to create your MediaPlayer. Create function calls the prepare() function which finalize your media and does not allow you to change AudioStreamType.

The solution is using setDataSource instead of create:

MediaPlayer mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.setLooping(true);
try {
   mp.setDataSource(getApplicationContext(), yourAudioUri);
   mp.prepare();
} catch (IOException e) {
   e.printStackTrace();
}
mp.start();

See this link for more information.

like image 45
Maedeh HM Avatar answered Sep 21 '22 18:09

Maedeh HM