Android - 从耳机播放音频 [英] Android - Play audio from earpiece
问题描述
我想使用 android MediaPlayer
将文件中的音频播放到手机的听筒(或连接的耳机)中.我尝试使用该线程中建议的 MODE_IN_CALL hack - Android - 播放音频通过听筒 但这并没有帮助.该应用还具有 android.permission.MODIFY_AUDIO_SETTINGS 权限,但对 m_amAudioManager.setSpeakerphoneOn(false);
的任何调用都将被忽略.
I'd like to play audio from a file into the phone's earpiece (or headset of that is connected) using the android MediaPlayer
. I tried using the MODE_IN_CALL hack as suggested in this thread - Android - Getting audio to play through earpiece but that did not help. The app also has the android.permission.MODIFY_AUDIO_SETTINGS permission, but any call to m_amAudioManager.setSpeakerphoneOn(false);
is ignored.
private AudioManager m_amAudioManager;
m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL);
m_amAudioManager.setSpeakerphoneOn(false);
在 MediaPlayer
上调用 setMode(AudioManager.STREAM_VOICE_CALL);
也没有帮助.
A call to setMode(AudioManager.STREAM_VOICE_CALL);
on the MediaPlayer
did not help either.
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
自 android 2.3 以来有什么改变来破坏这个代码吗?我在 Android ICS 上尝试了代码但没有成功.有人可以指出我正确的方向吗?到目前为止,音频始终通过免提电话播放.
Has something changed since android 2.3 to break this code ? I tried the code on Android ICS without any success. Can someone point me in the right direction ? The audio always plays from the speaker phone so far.
推荐答案
事实证明,正确的方法是通过以下代码.
It turns out the right way to do this is through the following code.
通过耳机播放
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
通过免提电话播放
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
就是这样.任何其他解决方案(包括此处发布的解决方案 -> Android - 播放音频听筒) 无法在不同设备上始终如一地工作.事实上,使用 MODE_IN_CALL 可确保您的音频从不在某些设备上播放.
That is it. Any other solution (Including the one posted here -> Android - Getting audio to play through earpiece) does not work consistently across devices. In fact using MODE_IN_CALL ensures that your audio will never play on certain devices.
注意:您需要重新准备媒体播放器以更改流.
Note: You will need to re-prepare the media player to change streams.
这篇关于Android - 从耳机播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!