如何在 android 4.0 中以编程方式打开/关闭扬声器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12036221/
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
how to turn speaker on/off programmatically in android 4.0
提问by Mayuri Khinvasara
I play a file through media player and I want to give options like speaker on/off, play though headset, bluetooth ,etc. I tried the below code which works well for android 2.2 but I want something that can also work for 2.2 and 4.0 both. Can you help me to programmatically turn the speaker on/off and playing via headphones?
我通过媒体播放器播放文件,我想提供诸如开/关扬声器、通过耳机播放、蓝牙等选项。我尝试了下面的代码,它适用于 android 2.2,但我想要一些也适用于 2.2 和 4.0 的东西。你能帮我以编程方式打开/关闭扬声器并通过耳机播放吗?
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
if(isOn){
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setMode(AudioManager.MODE_NORMAL);
}else{
//Seems that this back and forth somehow resets the audio channel
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setMode(AudioManager.MODE_IN_CALL);
}
audioManager.setSpeakerphoneOn(isOn);
P.S: I have given this permission in manifest:
PS:我已在清单中授予此权限:
android.permission.MODIFY_AUDIO_SETTINGS
回答by Michael
Something like this might work on some devices (I've only tested in on an XPeria P):
像这样的东西可能适用于某些设备(我只在 XPeria P 上测试过):
final static int FOR_MEDIA = 1;
final static int FORCE_NONE = 0;
final static int FORCE_SPEAKER = 1;
Class audioSystemClass = Class.forName("android.media.AudioSystem");
Method setForceUse = audioSystemClass.getMethod("setForceUse", int.class, int.class);
setForceUse.invoke(null, FOR_MEDIA, FORCE_SPEAKER);
// To get back to the default behaviour, use the combination FOR_MEDIA,FORCE_NONE.
The combination FOR_MEDIA, FORCE_SPEAKER
is typically only used internally to route the FM-radio audio to the loudspeaker (since the FM-radio requires you to have a wired headset / headphone plugged in to act as an antenna). Devices that don't have FM-radio functionality (or uses an alternative implementation) might ignore this combination of parameters, so this method would not work on such a device.
该组合FOR_MEDIA, FORCE_SPEAKER
通常仅在内部用于将 FM 无线电音频路由到扬声器(因为 FM 无线电要求您插入有线耳机/耳机以充当天线)。没有 FM 无线电功能(或使用替代实现)的设备可能会忽略此参数组合,因此此方法不适用于此类设备。
回答by Jatin Patel
AudioManager mAudioMgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
Button mVolumeButton = (Button)findViewById(R.id.btn_Volume);
mVolumeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mAudioMgr.isWiredHeadsetOn()){
mAudioMgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mAudioMgr.setWiredHeadsetOn(false);
mAudioMgr.setSpeakerphoneOn(true);
mAudioMgr.setMode(AudioManager.MODE_IN_COMMUNICATION);
Toast.makeText(getApplicationContext(), "SpeakerPhone On", Toast.LENGTH_LONG).show();
}else{
mAudioMgr.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioMgr.setSpeakerphoneOn(false);
mAudioMgr.setWiredHeadsetOn(true);
Toast.makeText(getApplicationContext(), "Wired Headset On", Toast.LENGTH_LONG).show();
}
}
});
回答by Jayesh Tembhekar
You can acquire either a rear speaker or a front earpiece at time.
您可以随时购买后置扬声器或前置听筒。
If no accessory connected;
如果没有连接附件;
Use audioManager.setMode(AudioManager.MODE_IN_CALL);
& audioManager.setSpeakerphoneOn(false);
to use front speaker/earpiece. But this would play audio in earpiece not on speaker. To use rear speaker, use audioManager.setMode(AudioManager.MODE_NORMAL);
& audioManager.setSpeakerphoneOn(true);
使用audioManager.setMode(AudioManager.MODE_IN_CALL);
&audioManager.setSpeakerphoneOn(false);
使用前置扬声器/听筒。但这会在听筒而不是扬声器上播放音频。要使用后置扬声器,请使用audioManager.setMode(AudioManager.MODE_NORMAL);
&audioManager.setSpeakerphoneOn(true);
If accessory connected; Use audioManager.setMode(AudioManager.MODE_IN_CALL); & audioManager.setSpeakerphoneOn(false); to use front speaker/earpiece. But this would play audio in earpiece not on speaker. To use rear speaker, use audioManager.setMode(AudioManager.MODE_IN_CALL);
& audioManager.setSpeakerphoneOn(true);
如果附件已连接;使用 audioManager.setMode(AudioManager.MODE_IN_CALL); & audioManager.setSpeakerphoneOn(false); 使用前置扬声器/听筒。但这会在听筒而不是扬声器上播放音频。要使用后置扬声器,请使用audioManager.setMode(AudioManager.MODE_IN_CALL);
&audioManager.setSpeakerphoneOn(true);
Note: Make sure audioManager.setWiredHeadsetOn(boolean on)
and audioManager.setBluetoothScoOn(boolean on)
set to false
to route audio via earpiece . And set either to true
to route audio accordingly.
注意:确保audioManager.setWiredHeadsetOn(boolean on)
并audioManager.setBluetoothScoOn(boolean on)
设置为false
通过听筒路由音频。并设置为相应true
地路由音频。
回答by aolphn
try follow code snippet:
尝试遵循代码片段:
//for spearphone on
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setSpeakerphoneOn(true);
//for headphone on
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.setSpeakerphoneOn(false);
BTW,I tested in Android 7.0(Redmi 4x) and it worked fine.
顺便说一句,我在 Android 7.0(Redmi 4x)上进行了测试,效果很好。
回答by myna
Problem solved. For all of you who are still searching for the answer. Well this is not a bug , but just a tricky thing . You need to use the PhoneStateListener
问题解决了。对于仍在寻找答案的所有人。嗯,这不是一个错误,而只是一件棘手的事情。您需要使用 PhoneStateListener
Using this guide : http://danielthat.blogspot.co.il/2013/06/android-make-phone-call-with-speaker-on.html
使用本指南:http: //danielthat.blogspot.co.il/2013/06/android-make-phone-call-with-speaker-on.html
回答by Sujith Ks
if u just want to open your speakerphone on u just write this line in oncreate() of your activity.
如果您只想打开免提电话,只需在活动的 oncreate() 中写下这一行。
static AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
回答by user9673947
Try This.
尝试这个。
AudioManager audioManager =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (isOn) {
isOn = false;
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setMode(AudioManager.MODE_NORMAL);
} else {
isOn = true;
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setMode(AudioManager.MODE_IN_CALL);
}
audioManager.setSpeakerphoneOn(isOn);