22 April 2012

How to change Audio Stream for MediaPlayer ?

STORY:
If you using MediaPlayer using default audio stream ,then below code works.

alarmPlayer = new MediaPlayer().create(this,R.raw.soundtest);
alarmPlayer.setLooping(false); alarmPlayer.prepare();
   alarmPlayer.start();


but for some odd reason you want use different stream ... for example ... alarm.
So you do think...let's set audio stream  and job done ....
setAudioStreamType(AudioManager.STREAM_ALARM);
... but it doesn't work.

SOLUTION:
If you writing app that use a lot MediaPlayer a lot,then ... study this:
http://developer.android.com/reference/android/media/MediaPlayer.html


So... for some odd reasons below solution will works ..
 alarmPlayer = new MediaPlayer();  
  alarmPlayer.setDataSource(this, Uri.parse("android.resource://org.ovh.pastor.alarmPlayerSoft/" + R.raw.soundtest));
alarmPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
alarmPlayer.setLooping(false);
alarmPlayer.prepare();
   alarmPlayer.start();

why ? I tried to understand  that,but  at 3 am doesn't help me to go through google docs and find out.
Be honest. It is unlogical for me, but it seems normal for Google.
:(.
Good night!