Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.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
使用SCO进行语音识别(ANDROID)时无蜂鸣音_Android_Bluetooth_Speech Recognition_Text To Speech - Fatal编程技术网

使用SCO进行语音识别(ANDROID)时无蜂鸣音

使用SCO进行语音识别(ANDROID)时无蜂鸣音,android,bluetooth,speech-recognition,text-to-speech,Android,Bluetooth,Speech Recognition,Text To Speech,我已经为我遇到的一个问题做了一个快速演示。当我运行以下代码时,文本被读取,但没有提示STT的蜂鸣音,也没有超时的双重蜂鸣音(错误7),但它正在运行 public class MainActivity extends AppCompatActivity { private final String TAG = this.getClass().getSimpleName(); TextToSpeech oTTS; HashMap<String, String>

我已经为我遇到的一个问题做了一个快速演示。当我运行以下代码时,文本被读取,但没有提示STT的蜂鸣音,也没有超时的双重蜂鸣音(错误7),但它正在运行

public class MainActivity extends AppCompatActivity {

    private final String TAG = this.getClass().getSimpleName();

    TextToSpeech oTTS;
    HashMap<String, String> params = new HashMap<>();
    String sTextToSpeak;
    String sTextFromSpeech;
    SpeechRecognizer sr;
    Intent iSRIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final AudioManager tmpAm = (AudioManager) getApplicationContext()
                .getSystemService(Context.AUDIO_SERVICE);

        BroadcastReceiver scoBR = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                final String TAG = this.getClass().getSimpleName();

                Log.i(TAG, "onReceive");

                // SCO Connected
                if (intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1) ==
                        AudioManager.SCO_AUDIO_STATE_CONNECTED) {

                    Log.i(TAG, "SCO Connected");

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    Log.i(TAG, "Speaking:" + sTextToSpeak);

                    // TODO fix deprecation
                    //noinspection deprecation
                    oTTS.speak(sTextToSpeak, TextToSpeech.QUEUE_ADD, params);
                }

                // SCO Disconnected
                if (intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1) ==
                        AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {

                    Log.i(TAG, "SCO Disconnected");
                }
            }
        };

        // Start scoBroadcastReceiver
        try {
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
            registerReceiver(scoBR, intentFilter);
            Log.i(TAG, "SCOBroadcastReceiver registered");
        } catch (Exception e) {
            Log.i(TAG, "SCOBroadcastReceiver already registered");
        }

        oTTS = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {

                Log.i(TAG, "onInit called");

                if (status == TextToSpeech.SUCCESS) {

                    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "utteranceId");

                    params.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
                            String.valueOf(AudioManager.STREAM_VOICE_CALL));

                    Log.i(TAG, "TTS initialized");

                    Log.i(TAG, "Start SCO");

                    tmpAm.startBluetoothSco();

                } else {

                    Log.i(TAG, "Couldn't initialize TTS");
                }
            }
        });

        oTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {

            @Override
            public void onStart(String s) {

                Log.i(TAG, "UtteranceProgressListener onStart called");
            }

            @Override
            public void onDone(String s) {

                Log.i(TAG, "UtteranceProgressListener onDone called");

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Log.i(TAG, "Calling sr");

                        sr.startListening(iSRIntent);
                    }
                });
            }

            @Override
            public void onError(String s) {

                Log.i(TAG, "UPL onError:" + s);
            }
        });

        sTextToSpeak = "This is a test";

        sr = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
        sr.setRecognitionListener(new RecognitionListener() {

            @Override
            public void onReadyForSpeech(Bundle bundle) {
                Log.i(TAG, "onReadyForSpeech");
            }

            @Override
            public void onBeginningOfSpeech() {
                Log.i(TAG, "onBeginningOfSpeech");
            }

            @Override
            public void onRmsChanged(float rmsDb) {
                //Log.i(TAG, "rmsDB:"+rmsDb);
            }

            @Override
            public void onBufferReceived(byte[] buffer) {
                Log.i(TAG, "onBufferReceived");
            }

            @Override
            public void onEndOfSpeech() {
                Log.i(TAG, "onEndOfSpeech");
            }

            @Override
            public void onError(int error) {

                Log.i(TAG, "onError:" + error);
                tmpAm.stopBluetoothSco();
            }

            @Override
            public void onResults(Bundle results) {

                ArrayList data = results.getStringArrayList(
                        SpeechRecognizer.RESULTS_RECOGNITION);
                if (data != null) {
                    sTextFromSpeech = data.get(0).toString();
                } else {
                    sTextFromSpeech = "";
                }

                Log.i(TAG, "onResults:" + sTextFromSpeech);
                tmpAm.stopBluetoothSco();
            }

            @Override
            public void onPartialResults(Bundle bundle) {

                Log.i(TAG, "onPartialResults:" + sTextFromSpeech);
                tmpAm.stopBluetoothSco();
            }

            @Override
            public void onEvent(int eventType, Bundle params) {

                Log.i(TAG, "onEvent:" + eventType);
            }
        });

        iSRIntent = new Intent(RecognizerIntent.ACTION_VOICE_SEARCH_HANDS_FREE);
        iSRIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        iSRIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        iSRIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak something...");
        iSRIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
        iSRIntent.putExtra("android.speech.extra.DICTATION_MODE", false);
        iSRIntent.putExtra("android.speech.extra.MODE", 1);
        iSRIntent.putExtra("android.speech.extra.PREFER_OFFLINE", false);
        iSRIntent.putExtra("android.speech.extra.SUGGESTIONS_ENABLED", false);
        iSRIntent.putExtra("android.speech.extra.PROFANITY_FILTER", true);
        iSRIntent.putExtra("android.speech.extra.AUDIO_ENCODING_REQUESTED", false);
    }

    @Override
    public void onDestroy() {

        super.onDestroy();

        Log.i(TAG, "onDestroy called");

        if (oTTS != null) {
            oTTS.stop();
            oTTS.shutdown();
        }
    }
}
public类MainActivity扩展了AppCompatActivity{
私有最终字符串标记=this.getClass().getSimpleName();
texttospeechots;
HashMap params=新的HashMap();
弦干尖;
字符串sTextFromSpeech;
语音识别器;
意图是意图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终AudioManager tmpAm=(AudioManager)getApplicationContext()
.getSystemService(Context.AUDIO_服务);
BroadcastReceiver scoBR=新的BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
最后一个字符串标记=this.getClass().getSimpleName();
Log.i(标签“onReceive”);
//上合组织连接
if(intent.getIntExtra(AudioManager.EXTRA\u SCO\u AUDIO\u STATE,-1)==
AudioManager.SCO_音频_状态_已连接){
Log.i(标签“SCO连接”);
试一试{
睡眠(500);
}捕捉(中断异常e){
e、 printStackTrace();
}
Log.i(标签“说话:”+sTextToSpeak);
//TODO修复弃用
//无检查弃用
oTTS.speak(sTextToSpeak,TextToSpeech.QUEUE_ADD,params);
}
//上合组织断开
if(intent.getIntExtra(AudioManager.EXTRA\u SCO\u AUDIO\u STATE,-1)==
AudioManager.SCO_音频_状态_断开连接){
Log.i(标签“SCO断开”);
}
}
};
//启动广播接收机
试一试{
IntentFilter IntentFilter=新的IntentFilter();
intentFilter.addAction(AudioManager.ACTION\u SCO\u AUDIO\u STATE\u更新);
注册接收者(scoBR、意向过滤器);
Log.i(标记“已注册的接收器”);
}捕获(例外e){
Log.i(标记“已注册的接收器”);
}
oTTS=newtexttospeech(getApplicationContext(),newtexttospeech.OnInitListener()){
@凌驾
公共无效onInit(int状态){
Log.i(标记“onInit called”);
if(status==TextToSpeech.SUCCESS){
参数put(TextToSpeech.Engine.KEY_PARAM_outrance_ID,“outranceid”);
参数输入(TextToSpeech.Engine.KEY_参数流,
String.valueOf(AudioManager.STREAM_VOICE_CALL));
Log.i(标记“TTS已初始化”);
Log.i(标签“启动SCO”);
tmpAm.startBluetoothSco();
}否则{
Log.i(标记“无法初始化TTS”);
}
}
});
setOnPatternanceProgressListener(新的话语进程Listener(){
@凌驾
公共void onStart(字符串s){
Log.i(标记“UtteranceProgressListener-onStart-called”);
}
@凌驾
公共无效onDone(字符串s){
Log.i(标记“UtteranceProgressListener-onDone called”);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Log.i(标签“呼叫sr”);
高级监听主任(ISR);
}
});
}
@凌驾
公共void onError(字符串s){
Log.i(标签“UPL onError:+s”);
}
});
sTextToSpeak=“这是一项测试”;
sr=SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
sr.setRecognitionListener(新的RecognitionListener(){
@凌驾
ReadyForSpeech上的公共无效(捆绑){
Log.i(标签“onReadyForSpeech”);
}
@凌驾
开始时的公共无效fSpeech(){
Log.i(标签“onbeginingofspeech”);
}
@凌驾
在RMSCHANGED上的公共无效(浮动rmsDb){
//Log.i(标签“rmsDB:+rmsDB”);
}
@凌驾
已接收公共无效onBufferReceived(字节[]缓冲区){
Log.i(标记“onBufferReceived”);
}
@凌驾
公共无效onEndOfSpeech(){
Log.i(标记为“onEndOfSpeech”);
}
@凌驾
公共无效onError(内部错误){
Log.i(标记“onError:+错误);
tmpAm.stopbuetoothsco();
}
@凌驾
公共结果(捆绑结果){
ArrayList data=results.getStringArrayList(
语音识别器。结果(语音识别);
如果(数据!=null){
sTextFromSpeech=data.get(0.toString();
}否则{
sTextFromSpeech=“”;
}
Log.i(标记“onResults:+sTextFromSpeech”);
tmpAm.stopbuetoothsco();
}
@凌驾
公共void onPartialResults(Bundle){
Log.i(标记“onPartialResults:+sTextFromSpeech”);
tmpAm.stopbuetoothsco();
}
@凌驾
public void onEvent(int eventType,Bundle参数){
Log.i(标记“onEvent:+eventType”);
}
});
iSRIntent=新意图(识别器意图、行动、语音、搜索、免提);