Question

Actually on the net or stackoverflow I cant find the thing I need,maybe I do not searched the real keyword on google so I cant find my solution. I play a sound(.mp3) ,it works fine. code is here:

try {
MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.bell);
mPlayer.start();
} catch (Exception e) {
// TODO: handle exception
}   

My problem is when I take my phone sound is off or vibrate , this code runs and I hear the sound.How can I detect phone sound is not open so I can stop the player, Thanks in advance

Was it helpful?

Solution

You can use AudioManager.getStreamVolume http://developer.android.com/reference/android/media/AudioManager.html#getStreamVolume%28int%29 to fetch the current volume for the media player stream (See STREAM_MUSIC http://developer.android.com/reference/android/media/AudioManager.html#STREAM_MUSIC ), and AudioManager.setStreamVolume http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume%28int,%20int,%20int%29 to set it.

You can use the following snippet, using AudioManager:

AudioManager am = 
    (AudioManager) getSystemService(Context.AUDIO_SERVICE);

am.setStreamVolume(
    AudioManager.STREAM_MUSIC,
    am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
    0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top