Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android TextToSpeech on bluetooth SCO_Android_Google Text To Speech - Fatal编程技术网

Android TextToSpeech on bluetooth SCO

Android TextToSpeech on bluetooth SCO,android,google-text-to-speech,Android,Google Text To Speech,我试图将TTS输出路由到外部蓝牙SCO设备(与本地扬声器和麦克风配合使用效果良好),但无法播放 我正在为AudioManager设置如下路线- AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); audioManager.startBluetoothSco(); audioManager.setBluetoothScoOn(true); 这些话语是用这种方法演奏出来的- priv

我试图将TTS输出路由到外部蓝牙SCO设备(与本地扬声器和麦克风配合使用效果良好),但无法播放

我正在为AudioManager设置如下路线-

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.startBluetoothSco();
audioManager.setBluetoothScoOn(true);
这些话语是用这种方法演奏出来的-

private void say(String text, String utteranceId) {
    Log.d(TAG, "Saying: " + text);
    final Bundle ttsParams = new Bundle();
    ttsParams.putInt(TextToSpeech.Engine.KEY_PARAM_STREAM, AudioManager.STREAM_VOICE_CALL);
    mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, ttsParams,  utteranceId);
}
扬声器没有声音。如果我不把它设置为true,它可以很好地与内置扬声器配合使用

使用这个类:

public abstract class BluetoothHeadsetUtils {
    private AudioManager mAudioManager;
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothHeadset mBluetoothHeadset;
    private BluetoothDevice mConnectedHeadset;
    private Context mContext;
    private CountDownTimer mCountDown;
    private BroadcastReceiver mHeadsetBroadcastReceiver;
    private BluetoothProfile.ServiceListener mHeadsetProfileListener;
    private boolean mIsCountDownOn;
    private boolean mIsOnHeadsetSco;
    private boolean mIsStarted;
    private boolean receiverRegistered;

    public BluetoothHeadsetUtils(final Context context) {

        mHeadsetBroadcastReceiver = new BroadcastReceiver() {
            public void onReceive(final Context context, final Intent intent) {
                final String action = intent.getAction();
                if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
                    final int intExtra = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset
                            .STATE_DISCONNECTED);
                    if (intExtra == BluetoothHeadset.STATE_CONNECTED) {
                        BluetoothHeadsetUtils.this.mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice
                                .EXTRA_DEVICE);
                        BluetoothHeadsetUtils.this.mIsCountDownOn = true;
                        BluetoothHeadsetUtils.this.mCountDown.start();
                        BluetoothHeadsetUtils.this.onHeadsetConnected();
                    } else if (intExtra == BluetoothHeadset.STATE_DISCONNECTED) {
                        if (BluetoothHeadsetUtils.this.mIsCountDownOn) {
                            BluetoothHeadsetUtils.this.mIsCountDownOn = false;
                            BluetoothHeadsetUtils.this.mCountDown.cancel();
                        }
                        BluetoothHeadsetUtils.this.mConnectedHeadset = null;
                        BluetoothHeadsetUtils.this.onHeadsetDisconnected();
                    }
                } else {
                    final int intExtra2 = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset
                            .STATE_AUDIO_DISCONNECTED);
                    if (intExtra2 == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
                        BluetoothHeadsetUtils.this.mIsOnHeadsetSco = true;
                        if (BluetoothHeadsetUtils.this.mIsCountDownOn) {
                            BluetoothHeadsetUtils.this.mIsCountDownOn = false;
                            BluetoothHeadsetUtils.this.mCountDown.cancel();
                        }
                        BluetoothHeadsetUtils.this.onScoAudioConnected();
                        return;
                    }
                    if (intExtra2 == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) {
                        BluetoothHeadsetUtils.this.mIsOnHeadsetSco = false;
                        BluetoothHeadsetUtils.this.mBluetoothHeadset.stopVoiceRecognition(BluetoothHeadsetUtils.this
                                .mConnectedHeadset);
                        BluetoothHeadsetUtils.this.onScoAudioDisconnected();
                    }
                }
            }
        };
        mHeadsetProfileListener = new BluetoothProfile.ServiceListener() {
            public void onServiceConnected(final int n, final BluetoothProfile bluetoothProfile) {
                BluetoothHeadsetUtils.this.mBluetoothHeadset = (BluetoothHeadset) bluetoothProfile;
                final List<BluetoothDevice> connectedDevices = BluetoothHeadsetUtils.this.mBluetoothHeadset
                        .getConnectedDevices();
                if (connectedDevices.size() > 0) {
                    BluetoothHeadsetUtils.this.mConnectedHeadset = connectedDevices.get(0);
                    BluetoothHeadsetUtils.this.onHeadsetConnected();
                    BluetoothHeadsetUtils.this.mIsCountDownOn = true;
                    BluetoothHeadsetUtils.this.mCountDown.start();
                } else {
                    BluetoothHeadsetUtils.this.noHeadsetConnected();
                }
                IntentFilter filter = new IntentFilter();
                filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
                filter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
                BluetoothHeadsetUtils.this.mContext.registerReceiver(BluetoothHeadsetUtils.this
                        .mHeadsetBroadcastReceiver, filter);
                receiverRegistered = true;
            }

            public void onServiceDisconnected(final int n) {
                BluetoothHeadsetUtils.this.stop();
            }
        };

        mCountDown = new CountDownTimer(10000L, 1000L) {
            public void onFinish() {
                BluetoothHeadsetUtils.this.mIsCountDownOn = false;
                onTimeout();
            }

            public void onTick(final long n) {
                BluetoothHeadsetUtils.this.mBluetoothHeadset.startVoiceRecognition(BluetoothHeadsetUtils.this
                        .mConnectedHeadset);
            }
        };
        mContext = context;
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mAudioManager = (AudioManager) this.mContext.getSystemService(Context.AUDIO_SERVICE);
    }

    private boolean startBluetooth() {
        return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled() && mAudioManager
                .isBluetoothScoAvailableOffCall() &&
                mBluetoothAdapter.getProfileProxy(mContext, mHeadsetProfileListener, BluetoothProfile.HEADSET);
    }

    public boolean isOnHeadsetSco() {
        return mIsOnHeadsetSco;
    }

    protected void onTimeout() {
    }

    protected void noHeadsetConnected() {
    }

    protected void onHeadsetConnected() {
    }

    protected void onHeadsetDisconnected() {
    }

    protected void onScoAudioConnected() {
    }

    protected void onScoAudioDisconnected() {
    }

    @UiThread
    public boolean start() {
        if (!mIsStarted) {
            mIsStarted = true;
            mIsStarted = startBluetooth();
        }
        return mIsStarted;
    }

    @UiThread
    public void stop() {
        if (mIsStarted) {
            mIsStarted = false;
            stopBluetooth();
        }
    }

    private void stopBluetooth() {
        if (mIsCountDownOn) {
            mIsCountDownOn = false;
            mCountDown.cancel();
        }
        try {
            if (receiverRegistered) {
                mContext.unregisterReceiver(mHeadsetBroadcastReceiver);
            }
        } catch (IllegalArgumentException ignored) {
        } finally {
            receiverRegistered = false;
        }
        if (mBluetoothHeadset != null) {
            mBluetoothHeadset.stopVoiceRecognition(mConnectedHeadset);
            mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
            mBluetoothHeadset = null;
        }
        mIsOnHeadsetSco = false;
    }
}
公共抽象类BluetoothHeadsetUtils{
私人音频经理;
私人蓝牙适配器mBluetoothAdapter;
私人蓝牙耳机;
私人蓝牙设备mConnectedHeadset;
私有上下文;
专用倒计时器mCountDown;
私人广播接收机mhead-castereceiver;
私有BluetoothProfile.ServiceListener mHeadsetProfileListener;
私有布尔错误计数;
私有布尔mIsOnHeadsetSco;
私有布尔值启动错误;
私人破产管理人注册;
公共BluetoothHeadsetUtils(最终上下文){
mHeadsetBroadcastReceiver=新广播接收器(){
公共void onReceive(最终上下文、最终意图){
最后一个字符串action=intent.getAction();
if(action.equals(蓝牙耳机.action\u CONNECTION\u STATE\u CHANGED)){
final int intExtra=intent.getIntExtra(蓝牙耳机.EXTRA_状态,蓝牙耳机
.STATE_(已断开连接);
if(intExtra==蓝牙耳机。状态_已连接){
BluetoothHeadsetUtils.this.mConnectedHeadset=intent.getParcelableExtra(蓝牙设备
1.额外装置);
BluetoothHeadsetUtils.this.mIsCountDownOn=true;
BluetoothHeadsetUtils.this.mCountDown.start();
BluetoothHeadsetUtils.this.onHeadsetConnected();
}else if(intExtra==蓝牙耳机。状态_已断开){
if(BluetoothHeadsetUtils.this.mIsCountDownOn){
BluetoothHeadsetUtils.this.mIsCountDownOn=false;
BluetoothHeadsetUtils.this.mCountDown.cancel();
}
BluetoothHeadsetUtils.this.mConnectedHeadset=null;
BluetoothHeadsetUtils.this.onHeadsetDisconnected();
}
}否则{
final int intExtra2=intent.getIntExtra(蓝牙耳机.EXTRA_状态,蓝牙耳机
.状态(音频已断开);
if(intExtra2==蓝牙耳机。状态\音频\已连接){
BluetoothHeadsetUtils.this.mIsOnHeadsetSco=true;
if(BluetoothHeadsetUtils.this.mIsCountDownOn){
BluetoothHeadsetUtils.this.mIsCountDownOn=false;
BluetoothHeadsetUtils.this.mCountDown.cancel();
}
BluetoothHeadsetUtils.this.onCoAudioConnection();
返回;
}
如果(intExtra2==蓝牙耳机。状态\音频\断开连接){
BluetoothHeadsetUtils.this.mIsOnHeadsetSco=false;
BluetoothHeadsetUtils.this.mblueothHeadset.stopVoiceRecognition(BluetoothHeadsetUtils.this
.mConnectedHeadset);
BluetoothHeadsetUtils.this.onScoAudioDisconnected();
}
}
}
};
mHeadsetProfileListener=新的BluetoothProfile.ServiceListener(){
服务连接上的公共无效(最终int n,最终BluetoothProfile BluetoothProfile){
BluetoothHeadsetUtils.this.mBluetoothHeadset=(BluetoothHeadset)bluetoothProfile;
最终列表connectedDevices=BluetoothHeadsetUtils.this.mBluetoothHeadset
.getConnectedDevices();
如果(connectedDevices.size()>0){
BluetoothHeadsetUtils.this.mConnectedHeadset=connectedDevices.get(0);
BluetoothHeadsetUtils.this.onHeadsetConnected();
BluetoothHeadsetUtils.this.mIsCountDownOn=true;
BluetoothHeadsetUtils.this.mCountDown.start();
}否则{
BluetoothHeadsetUtils.this.noHeadsetConnected();
}
IntentFilter=newintentfilter();
filter.addAction(蓝牙耳机。操作\连接\状态\更改);
filter.addAction(蓝牙耳机。操作\音频\状态\已更改);
BluetoothHeadsetUtils.this.mContext.registerReceiver(BluetoothHeadsetUtils.this
.mHead(接收装置、过滤器);
破产管理人注册=真实;
}
已断开连接的服务上的公共无效(最终整数n){
BluetoothHeadsetUtils.this.stop();
}
};
mCountDown=新的倒计时(10000L,1000L){
公共无效onFinish(){
BluetoothHeadsetUtils.this.mIsCountDownOn=false;
onTimeout();
}
公共void onTick(最终长n){
BluetoothHeadsetUtils.this.mblueothHeadset.startVoiceRecognition(BluetoothHeadsetUtils.this
.mConnectedHeadset);
}
};
mContext=上下文;
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
mAudioManager=(AudioManager)this.mContext.getSystemService(Context.AUDIO\u服务);
}
私有布尔startBluetooth(){
返回mBluetoothAdapter!=null&&mBluetoothAdapter.isEnabled()&&mAudioManager
.isBluetoothsAvailableOffCall()&&
mBluetoothAdapter.getProfileProxy(mContext,mHeadsetProfileList