简体   繁体   English

在Android中将语音路由到蓝牙

[英]Route voice to bluetooth in android

i want to route the voice to Bluetooth. 我想将语音路由到蓝牙。

below code is my Player. 下面的代码是我的播放器。

    AudioTrack at;
try {
            minbuffer = AudioTrack.getMinBufferSize(8000,
                    AudioFormat.CHANNEL_OUT_MONO,
                    AudioFormat.ENCODING_PCM_16BIT);
            if (minbuffer < VOICE_SPEECH_SIZE)
                minbuffer = VOICE_SPEECH_SIZE;
            at = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 8000,
                    AudioFormat.CHANNEL_OUT_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, minbuffer,
                    AudioTrack.MODE_STREAM);
            at.play();

            while (PlayOutblinker == Thread.currentThread()) {
                byte[] tt = vbuff.take();
                at.write(tt, 0, tt.length);
            }
        } finally {
            at.stop();
            at.release();
            at = null;
        }

i searched and find below : 我搜索并找到以下内容:

public static void SetRouteBT(Context context, boolean isRoute) {// TODO
    AudioManager mAudioManager = (AudioManager) context
            .getSystemService(Context.AUDIO_SERVICE);
    mAudioManager.setMode(AudioManager.MODE_IN_CALL);
    mAudioManager.startBluetoothSco();
    mAudioManager.setBluetoothScoOn(isRoute);
}

but it does not work. 但它不起作用。 when i setBluetoothScoOn(true) voice does not route but disconnect from speaker and microphone. 当我setBluetoothScoOn(true)语音不会路由,但会与扬声器和麦克风断开连接。 where is the problem? 问题出在哪儿?

the function should change to below code : 该功能应更改为以下代码:

    public static void SetRouteSco(Context context, boolean isRoute) {
    AudioManager mAudioManager = (AudioManager) context
            .getSystemService(Context.AUDIO_SERVICE);
    mAudioManager.setBluetoothScoOn(isRoute);
    if (isRoute)
        mAudioManager.startBluetoothSco();
    else
        mAudioManager.stopBluetoothSco();
}

and i need below permissions too. 我也需要以下权限。

<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM