tags:

views:

152

answers:

1

Hi all,

I'm trying to create a way to adjust volume settings for each of the different streams (media, notification, ringtone, etc) and have a way to preview the output sound level of each stream. I believe I have the correct implementation, but when I set the output stream type, there is no sound that plays.

Here is the code that correctly plays the user's selected alarm sound:

Uri mediaUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
MediaPlayer mp=MediaPlayer.create(getApplicationContext(), mediaUri);
//mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.start();`

That commented out line is what is causing me problems. I would like to hear the alarm sound at the volume levels of the different audio streams, but when I include that line for STREAM_ALARM or any other audio stream, no sound at all plays. Any ideas what could be going on here?

+1  A: 

Okay, I found the solution after a bit more testing and it looks like this, in case anyone else runs into the same problem I was having. The MODIFY_AUDIO_SETTINGS permission is needed in the Manifest for this to work.

AudioManager am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_NORMAL);
MediaPlayer mp=new MediaPlayer();
Uri ringtoneUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
try
{
    mp.setDataSource(getApplicationContext(), ringtoneUri);
    mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
    mp.prepare();
    mp.start();
}
catch(Exception e)
{
    //exception caught in the end zone
}
Ryan Zink
Please, accept your answer, so others will know that solution is found.
Konstantin Burov
I will, it looks like SO makes you wait 24 hours before accepting your own answer.
Ryan Zink