Android 通过耳机播放音乐时通过扬声器播放声音
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12017297/
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
Playing sound over speakers while playing music through headphones
提问by Ravi Tej Vupasi
I have an AudioTrack
streaming via headphones. I have to send a SoundPool
to built-in speakers only, without interrupting the AudioTrack
playing over the headphones. Any hacks gangsters?
我AudioTrack
通过耳机进行流媒体播放。我只需要将 a 发送SoundPool
到内置扬声器,而不会中断AudioTrack
耳机的播放。有黑帮大佬吗?
回答by Michael
Many Android devices use a single output thread in the AudioFlinger / audio HAL for all local playback (earpiece, speaker, wired headset/headphones), making different routing of two tracks simultaneously impossible (which is why on many devices the media streams are forcibly muted if a notification is played and you've got a wired headset attached; because otherwise you'd hear the music in the loudspeaker while the notification is played).
许多 Android 设备在 AudioFlinger / 音频 HAL 中使用单个输出线程进行所有本地播放(听筒、扬声器、有线耳机/耳机),从而不可能同时对两个音轨进行不同的路由(这就是为什么在许多设备上媒体流被强制静音的原因)如果播放通知并且您连接了有线耳机;否则您会在播放通知时听到扬声器中的音乐)。
On some devices it might be possible to do what you're looking for if you manage to do a setForceUse(FOR_MEDIA, FORCE_SPEAKER)
and use the MUSIC
stream type for the stuff you want to play in the loudspeaker, and the VOICE_CALL
stream type for the stuff that you want to play in the wired headset.
I'm not sure if there's any way for an application to perform that setForceUse
call though. Perhaps you can get at the handleMessage
method of the AudioService
class through reflection and send it an MSG_SET_FORCE_USE
message.. I've never tried it myself so it might fail miserably.
在某些设备上,如果你能做到一个有可能做你要找的内容setForceUse(FOR_MEDIA, FORCE_SPEAKER)
,并使用MUSIC
您希望在扬声器播放的东西流类型,并VOICE_CALL
要发挥流类型的东西有线耳机。
我不确定应用程序是否有办法执行该setForceUse
调用。也许你可以通过反射获得类的handleMessage
方法AudioService
并发送MSG_SET_FORCE_USE
消息。我自己从未尝试过,所以它可能会失败。
EDIT:I've now tested the setForceUse way of forcing MEDIA streams to the loudspeaker while a wired headset is attached on an actual device, and it does work (though I can't guarantee that it will work across all devices). The implementation was slightly different from what I described above. See my answer to how to turn speaker on/off programatically in android 4.0for the code I used.
编辑:我现在测试了 setForceUse 强制媒体流到扬声器的方法,而有线耳机连接在实际设备上,它确实有效(尽管我不能保证它可以在所有设备上工作)。实现与我上面描述的略有不同。对于我使用的代码,请参阅我对如何在 android 4.0 中以编程方式打开/关闭扬声器的回答。