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!
Try the following:
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
I don't know why this would happen, however the code below works. You should set the data source with
setDataSource()
instead of withcreate()
.This code works:
This code doesn't work:
The solution here was deprecated in API 22
I opened my own thread to figure this out.
Here is an updated working solution.
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.
The issue is you are using
MediaPlayer.create()
to create your MediaPlayer.Create
function calls theprepare()
function which finalize your media and does not allow you to changeAudioStreamType
.The solution is using
setDataSource
instead ofcreate
:See this link for more information.