我的要求是根据用户选择在蓝牙和手机扬声器之间切换音频。
下面是代码 fragment :

//AudioTrack for incoming audio to play as below:

    int mMaxJitter = AudioTrack.getMinBufferSize(8000,               AudioFormat.CHANNEL_OUT_MONO,AudioFormat.ENCODING_PCM_16BIT);
    new AudioTrack(AudioManager.STREAM_VOICE_CALL,8000,
           AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
           mMaxJitter, AudioTrack.MODE_STREAM);


//To register broadcast receiver for bluetooth audio routing
    IntentFilter ifil = new IntentFilter();
    ifil.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
    this.registerReceiver(<receiver instance>,ifil);

//To get AudioManager service
    AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

//Whenever user select to route audio to Bluetooth
    mAudioManager.setMode(AudioManager.MODE_IN_CALL);//tried setting with other mode also viz. MODE_NORMAL, MODE_IN_COMMUNICATION but no luck
    mAudioManager.startBluetoothSco();//after this I get AudioManager.SCO_AUDIO_STATE_CONNECTED state in the receiver
    mAudioManager.setBluetoothScoOn(true);
    mAudioManager.setSpeakerphoneOn(false);

//Whenever user select to route audio to Phone Speaker
    mAudioManager.setMode(AudioManager.MODE_NORMAL);
    mAudioManager.stopBluetoothSco();//after this I get      AudioManager.SCO_AUDIO_STATE_DISCONNECTED state in the receiver
    mAudioManager.setBluetoothScoOn(false);
    mAudioManager.setSpeakerphoneOn(true);

问题:
1.我可以路由音频但行为不一致,有时即使用户选择路由到蓝牙(蓝牙已连接)也会路由到手机扬声器
2.如果音频路由到手机扬声器,音量变低(请不要说检查手机音量)
3.只有几次我可以观察到音频路由是正确的,如果我重复它就会变得像我上面提到的那样奇怪。

安卓版本:Jellybean 4.3

有没有人遇到过类似的行为?

谢谢!

最佳答案

我得到了音频路由不一致的原因,这是因为我将手机扬声器设置为 false,而且我使用了不合适的模式......
以下组合对我有用:

//For BT
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(true);

//For phone ear piece
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.stopBluetoothSco();
mAudioManager.setBluetoothScoOn(false);
mAudioManager.setSpeakerphoneOn(false);

//For phone speaker(loadspeaker)
mAudioManager.setMode(AudioManager.MODE_NORMAL);
mAudioManager.stopBluetoothSco();
mAudioManager.setBluetoothScoOn(false);
mAudioManager.setSpeakerphoneOn(true);

安卓版本:4.3

谢谢!

关于安卓 : Switching audio between Bluetooth and Phone Speaker is inconsistent,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22770321/

10-09 01:36