Android MediaPlayer - how to play in the STREAM_ALARM?
Asked Answered
B

5

16

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!

Boccherini answered 14/8, 2011 at 13:36 Comment(0)
C
30

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();
Cerecloth answered 30/11, 2011 at 2:43 Comment(1)
better to write the uri like: Uri.parse("android.resource://"+mContext.getPackageName()+"/"+R.raw.sound)Reconnaissance
C
7

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.

Chrissychrist answered 4/1, 2017 at 0:38 Comment(0)
P
4

The solution here was deprecated in API 22

I opened my own thread to figure this out.

Here is an updated working solution.

mediaPlayerScan = new MediaPlayer();
try {
  mediaPlayerScan.setDataSource(getContext(),
          Uri.parse(getString(R.string.res_path) + R.raw.scan_beep));

  if (Build.VERSION.SDK_INT >= 21) {
    mediaPlayerScan.setAudioAttributes(new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_ALARM)
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .build());
  } else {
    mediaPlayerScan.setAudioStreamType(AudioManager.STREAM_ALARM);
  }
  mediaPlayerScan.prepare();
} catch (IOException e) {
  e.printStackTrace();
}
Publishing answered 15/6, 2018 at 20:4 Comment(1)
Found a simplier way (for SDK >= 21): MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.<sound>, new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_ALARM).build(), audioManager.generateAudioSessionId());Menadione
M
2

1. setAudioStreamType(int streamtype)

Must call this method before prepare() ;

2. MediaPlayer.create(Context context, int resid)

On success, prepare() will already have been called and must not be called again.

Municipalize answered 17/7, 2013 at 11:21 Comment(0)
G
0

Try the following:

player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.prepare();
player.start();

And why are you calling "audioManager.getStreamVolume(AudioManager.STREAM_ALARM);"? The value isn't stored in a variable, so it is rather useless ;)

I hope that helped

Gerdagerdeen answered 14/8, 2011 at 15:9 Comment(1)
I tried it with player.prepare() but it didn't make any difference. Still no audio played. I was storing the the stream volume to a variable which is used later in my app, I took out the assignment temporarily to help with debugging.Boccherini

© 2022 - 2024 — McMap. All rights reserved.