Android - 我可以静音当前播放的音频应用程序吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1993471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - can I mute currently playing audio applications?
提问by Christopher Orr
Hi I'm new to this. My self-teaching project is a small application which plays an audio news stream. To be effective, the application really needs to interrupt any currently playing media players (eg: Last FM, Imeem, music player, Spotify etc).
嗨,我是新手。我的自学项目是一个播放音频新闻流的小应用程序。为了有效,应用程序确实需要中断任何当前正在播放的媒体播放器(例如:Last FM、Imeem、音乐播放器、Spotify 等)。
I don't know what will be playing or what application will be playing it, but I want to ask them all to pause until I've done. Sounds rather bold, I know, but it must be possible, because when there's an incoming call, that's basically what happens.
我不知道将播放什么或将播放什么应用程序,但我想请他们暂停,直到我完成。我知道这听起来相当大胆,但它必须是可能的,因为当有来电时,基本上就是这样。
Can I send a message to all audio players asking them to pause for a moment? Could I spoof an incoming call, just for the time it takes?
我可以向所有音频播放器发送消息让他们暂停片刻吗?我可以欺骗一个来电,只是为了它需要的时间吗?
Thanks.
谢谢。
回答by Christopher Orr
Audio handling on Android is going to be pretty horrible for a while. The APIs are pretty weird, poorly documented, and keep changing/deprecating/breaking between versions. Even the AudioManager
code has FIXMEs in it.
一段时间内,Android 上的音频处理将变得非常糟糕。API 非常奇怪,文档不全,并且在版本之间不断更改/弃用/破坏。甚至AudioManager
代码中也有 FIXME。
Anyway, there are several stream types in Android (music, notifications, phone calls, etc.) and applications are meant to choose the appropriate one for playback. I imagine the majority of Android apps should use the music/media type (STREAM_TYPE_MUSIC
). You set this on your MediaPlayer
using the setAudioStreamType
method.
无论如何,Android 中有多种流类型(音乐、通知、电话等),应用程序旨在选择合适的一种进行播放。我想大多数 Android 应用程序都应该使用音乐/媒体类型 ( STREAM_TYPE_MUSIC
)。你在你的MediaPlayer
使用setAudioStreamType
方法上设置它。
The SDK does allow you to set a single stream typeas solo — causing all other streams to be muted — but I don't believe you can identify the audio being played back by particular applications and somehow pause/unpause it. Music applications in general will use the PhoneStateListener
to pause themselves when a call comes in.
SDK 确实允许您将单个流类型设置为独奏 - 导致所有其他流被静音 - 但我不相信您可以识别特定应用程序正在播放的音频并以某种方式暂停/取消暂停它。PhoneStateListener
当有来电时,音乐应用程序通常会使用来暂停自己。
So in your case, you could "borrow" the phone call stream for your MediaPlayer
and use the method call AudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true)
when playback begins, then un-solo the stream with false
when playback or your Activity
is done.
因此,在您的情况下,您可以为您“借用”电话呼叫流MediaPlayer
并AudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true)
在播放开始时使用方法调用,然后false
在播放或Activity
完成时取消对流的独奏。
I can tell you that this works, but I can't remember offhand whether you need to also set the audio modeto MODE_IN_CALL
when using the voice call stream (like this: AudioManager.setMode(AudioManager.MODE_IN_CALL)
). If you find that is required, then you need to make sure you return the mode to MODE_NORMAL
once playback completes, otherwise whenever you press the volume hard keys, it'll say "In-call volume"! However, if and when you do want to change back to MODE_NORMAL
, you must check that a genuine phone call isn't happening at that time...
我可以告诉你,这个工作,但我不记得随便你是否需要将音频也被设置模式来MODE_IN_CALL
使用语音通话流时(是这样的:AudioManager.setMode(AudioManager.MODE_IN_CALL)
)。如果您发现需要,那么您需要确保在MODE_NORMAL
播放完成后将模式返回到模式,否则每当您按下音量硬键时,它都会显示“通话音量”!但是,如果您确实想改回MODE_NORMAL
,则必须检查当时没有发生真正的电话...
Maybe you could use another stream type rather than the voice call one, but I'm just speaking from experience working on an app that could use either the speakerphone or the earpiece for audio playback, which requiresthe use of the voice call stream.
也许您可以使用另一种流类型而不是语音通话,但我只是从开发应用程序的经验谈起,该应用程序可以使用免提电话或听筒进行音频播放,这需要使用语音通话流。
Like I said, audio handling isn't particularly fun... ;)
就像我说的,音频处理并不是特别有趣......;)
回答by Christopher Orr
It has been 2 years. But I hope this could help someone:
已经2年了。但我希望这可以帮助某人:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, true);
Note:if you call setStreamMute(int, boolean)to mute the stream ntimes, you must call it ntimes again to unmute (use false
).
注意:如果您调用setStreamMute(int, boolean)将流静音n次,则必须再次调用n次以取消静音(使用false
)。
回答by yano
The appropriate way is to use the AudioManager.requestAudioFocus(...)
method
合适的方法是使用AudioManager.requestAudioFocus(...)
方法
回答by Maksym Gontar
You can try AudioManager.adjustVolumemethod:
您可以尝试AudioManager.adjustVolume方法:
AudioManager audioMan =
(AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioMan.adjustVolume(AudioManager.ADJUST_LOWER,
AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);