如何停止其他应用程序的声音?
我有一个应用程序需要播放声音,并且需要始终将音量设置为 100%(这是警报声)。我使用这段代码:
// 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();
它的工作原理就像我想要的那样:如果“多媒体音量”为 0%,则将其设置为 100% 并且播放我的声音。问题是,如果另一个应用程序正在播放声音(例如后台的音乐应用程序),该音量也会设置为 100%。所以它会以 100% 的速度一起播放我的声音和其他应用程序的声音。
我希望我的应用停止其他应用的所有多媒体声音,只听我的。这可能吗?
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(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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我意识到这是一个老问题,但我自己也做了同样的事情,并在解决问题时遇到了这个问题。
定位 API 8+ (Froyo)
如果您定位 API 8+,请查看 http://developer.android.com/training/managing-audio/audio-focus.html 其中讨论了使用音频焦点的想法,并且应该允许您执行以下操作你想要的。这使您可以请求成为唯一在流上播放音频的应用程序。
这是简单版本。
定位 API 7+ (Eclair)
但是,如果您像我一样并且想要定位 API 7+,则需要使用下面不太可靠的方法。
由于您正在播放闹钟,因此最好的选择是使用
AudioManager.STREAM_ALARM
而不是AudioManager.STREAM_MUSIC
,而且您离有效的方法也不远了。使用 setStreamSolo(int StreamType, boolean state) 方法来静音其他流,如果您使用警报流,这会更容易。据我所知,警报流的原因是 API 的工作方式造成的。您只能将整个流静音(请参阅最后)或除一个流之外的所有流(本例)。您无法使用 API 7 将特定应用程序或同一流上的其他应用程序静音。
停止时,清除资源
恢复初始音量会将流恢复到将其设置为 100% 之前的位置。
另外,您可以使用 setStreamMute 将特定流静音(int, boolean) 如果您想将特定流静音。例如,
使用“取消静音”请记住
,如果将其静音,则无法在媒体流上播放音频,而需要在另一个媒体流上播放。
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.
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 ofAudioManager.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.
When stopping, clear up the resources
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
Unmute it with
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.