如何停止其他应用程序的声音?

发布于 2024-10-19 01:44:03 字数 714 浏览 13 评论 0原文

我有一个应用程序需要播放声音,并且需要始终将音量设置为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪歌 2024-10-26 01:44:03

我意识到这是一个老问题,但我自己也做了同样的事情,并在解决问题时遇到了这个问题。

定位 API 8+ (Froyo)

如果您定位 API 8+,请查看 http://developer.android.com/training/managing-audio/audio-focus.html 其中讨论了使用音频焦点的想法,并且应该允许您执行以下操作你想要的。这使您可以请求成为唯一在流上播放音频的应用程序。

这是简单版本。

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+ (Eclair)

但是,如果您像我一样并且想要定位 API 7+,则需要使用下面不太可靠的方法。

由于您正在播放闹钟,因此最好的选择是使用 AudioManager.STREAM_ALARM 而不是 AudioManager.STREAM_MUSIC,而且您离有效的方法也不远了。使用 setStreamSolo(int StreamType, boolean state) 方法来静音其他流,如果您使用警报流,这会更容易。

据我所知,警报流的原因是 API 的工作方式造成的。您只能将整个流静音(请参阅最后)或除一个流之外的所有流(本例)。您无法使用 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();

停止时,清除资源

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);

恢复初始音量会将流恢复到将其设置为 100% 之前的位置。

另外,您可以使用 setStreamMute 将特定流静音(int, boolean) 如果您想将特定流静音。例如,

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

使用“取消静音”请记住

am.setStreamMute(AudioManager.STREAM_MUSIC, false);

,如果将其静音,则无法在媒体流上播放音频,而需要在另一个媒体流上播放。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文