How to stop the sound of other applications?

I have an application that needs to play sound, and it needs to set the volume to 100% ALWAYS (this is an audio signal). 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(this, R.raw.alarm_sound); mp.setLooping(true); mp.prepare(); mp.start(); 

It works the way I want: if the "Multimedia Volume" is 0%, it is set to 100%, and my sound plays. The problem is that if another application plays sound (for example, the Music application in the background), this volume is also set to 100%. Thus, it reproduces my sound and other application sounds together 100%.

I want my application to stop all multimedia sounds of other applications, so that I can only listen to mine. Is it possible?

+6
android audio volume android-audiomanager
source share
1 answer

I understand that this is an old question, but I just did the same thing myself and stumbled upon it, realizing it.

API 8+ Targeting (Froyo)

If you are targeting API 8+, check out http://developer.android.com/training/managing-audio/audio-focus.html , which discusses the use of the idea of ​​focusing sound and you should let you do what you want. This allows you to request a single application that plays sound in a stream.

This is a 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. } 

API 7+ targeting (Eclair)

However, if you are like me and you want to target API 7+, you will need to use the less reliable method below.

Since you play the alarm, it is best to use AudioManager.STREAM_ALARM instead of AudioManager.STREAM_MUSIC , and you are also just around the corner. Use setStreamSolo (int streamType, boolean state) to disable other streams, which is easier if you use an alarm stream.

As far as I know, the reason is that the alarm flow is caused by how the API works. you can disable only the entire stream (see the end) or all but one stream (this example). You cannot disable specific applications or other applications in the same thread using 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(); 

Clean up resources when stopped

 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 the initial volume will return the stream to where it was before you set it to 100%.

In addition, you can disable a specific stream setStreamMute (int, boolean) if you want to disable a specific stream. for example

 AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); am.setStreamMute(AudioManager.STREAM_MUSIC, true); 

Enable it with

 am.setStreamMute(AudioManager.STREAM_MUSIC, false); 

Keep in mind that you cannot play your sound in the media stream if you turn it off and you need to play on another one.

+6
source share

All Articles