gpt4 book ai didi

android - 通过蓝牙播放 TTS

转载 作者:行者123 更新时间:2023-11-29 18:02:15 25 4
gpt4 key购买 nike

我有一个小应用程序,如果已连接,我希望能够将 TTS 音频发送到扬声器或蓝牙耳机。它在免提电话模式下工作正常,但 BT 就不行了。知道如何解决这个问题吗?代码 fragment 如下:'

private void speak(String s){

        HashMap<String, String> hash = new HashMap<String, String>(); 

hash.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_SYSTEM));



AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.startBluetoothSco();
audioManager.setBluetoothScoOn(true);

tts.speak(s, TextToSpeech.QUEUE_ADD, hash);



}

'

最佳答案

Using the Android RecognizerIntent with a bluetooth headset 使用我的答案

在您声明 private void speak(String s) 的 Activity 中添加类成员

BluetoothHeadsetUtils mBluetoothHelper;
TelephonyManager mTelManager;
MyPhoneStateListener mPhoneStateListener = new MyPhoneStateListener();

在同一 Activity onCreate() 添加

@Override
public void onCreate()
{
mBluetoothHelper = new BluetoothHeadsetUtils(this);
mTelManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mTelManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

并为activity添加onResume()和onPause

@Override
onResume()
{
mBluetoothHelper.start();
}

@Override
onPause()
{
mBluetoothHelper.stop();
}

您可以将您的 speak(String s) 定义为

public void speak(String s, String utteranceId) // utteranceId can be an empty string
{
HashMap<String, String> myHashRender = new HashMap<String, String>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
if (mBluetoothHelper.isOnHeadsetSco())
{
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
String.valueOf(AudioManager.STREAM_VOICE_CALL));
}
tts.speak(s, TextToSpeech.QUEUE_ADD, myHashRender);
}

使用电话状态监听器代替广播接收器

protected class MyPhoneStateListener extends PhoneStateListener
{

@Override
public void onCallStateChanged(int state, String incomingNumber)
{

switch (state)
{
case TelephonyManager.CALL_STATE_RINGING:

mAudioManager.setStreamMute(AudioManager.STREAM_RING, true);
speak("whatever you want here", "ringing"); //$NON-NLS-1$

Log.d(TAG, "ringing cancel recognizer"); //$NON-NLS-1$

break;

case TelephonyManager.CALL_STATE_OFFHOOK:


Log.d(TAG, "offhook cancel recognizer"); //$NON-NLS-1$

break;

case TelephonyManager.CALL_STATE_IDLE:


new Thread(new Runnable()
{
@Override
public void run()
{
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{

}
mBluetoothHelper.start();
}
}).start();
Log.d(TAG, "call state idle"); //$NON-NLS-1$

}


}
}

在您的 Activity 或服务中添加实现 OnUtteranceCompletedListener 并添加以下代码

@Override
public void onUtteranceCompleted(String utteranceId)
{
if (utteranceId.equals("ringing"))
{
mAudioManager.setStreamMute(AudioManager.STREAM_RING, false);
mBluetoothHelper.stop();
}
}

关于android - 通过蓝牙播放 TTS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15325641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com