How to stop other apps sounds?
I have an app that needs to play a sound, and it needs to set the volume at 100% ALWAYS (it's an alarm sound). I use this piece of code:
// First I set the volume to 100%
AudioManager mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
// Now I play the sound
MediaPlayer mp = MediaPlayer.create开发者_如何学Go(this, R.raw.alarm_sound);
mp.setLooping(true);
mp.prepare();
mp.start();
It works like I want: if the "Multimedia Volume" is 0%, it is set to 100% and my sound is played. The problem is that if another app is playing a sound (like the Music app in background), that volume is set to 100% too. So it plays my sound and other apps sounds together at 100%.
I want my app to stop all multimedia sounds of other apps to listen only mine. Is it possible?
I realize this is an old question, but I just did this same thing myself and came across this while figuring it out.
Targeting API 8+ (Froyo)
If you're targeting API 8+, take a look at http://developer.android.com/training/managing-audio/audio-focus.html which discusses using the idea of audio focus and should allow you to do what you want. This lets you request to be the only app playing audio on a stream.
This is simple version.
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback
int result = am.requestAudioFocus(afChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// Start playback.
}
Targeting API 7+ (Eclair)
However if you're like me and you want to target API 7+, you'll need to use the less robust method below.
Since you're playing an alarm, your best bet is to use AudioManager.STREAM_ALARM
instead of AudioManager.STREAM_MUSIC
and you're also not far off from what works. Use the setStreamSolo(int streamType, boolean state) method to mute other streams, which is easier if you use the alarm stream.
As far as I'm aware, that is the reason for the alarm stream is due to the way API works. you can only mute an entire stream (see at the end) or all except one stream (this example). You can't mute specific applications or other apps on the same stream with API 7.
AudioManager mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
// save initial volume
int mInitialVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
// set the alarm stream to 100%
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
// Now play the sound
MediaPlayer mp = MediaPlayer.create(this, R.raw.alarm_sound);
// change here is to set it to play on the alarm stream
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.setLooping(true);
mp.prepare();
// setSoloStream(...) will mute all other streams
mAudioManager.setStreamSolo(AudioManager.STREAM_ALARM, true);
mp.start();
When stopping, clear up the resources
mp.stop();
mp.reset();
mp.release();
// release the solo steam to unmute the other streams
mAudioManager.setStreamSolo(AudioManager.STREAM_ALARM, false);
// restore the initial stream volume
mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mInitialVolume, AudioManager.FLAG_ALLOW_RINGER_MODES);
Restoring initial volume will put the stream back where it was prior to you setting it to 100%.
Also, you can mute a specific stream with setStreamMute(int, boolean) if you want to mute a specific stream. For example
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, true);
Unmute it with
am.setStreamMute(AudioManager.STREAM_MUSIC, false);
Keep in mind that you can't play your audio on the media stream if you mute it and will need to play on another.
精彩评论