Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.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 文本到语音API未调用SetOnPatternanceCompletedListener_Android_Text To Speech - Fatal编程技术网

Android 文本到语音API未调用SetOnPatternanceCompletedListener

Android 文本到语音API未调用SetOnPatternanceCompletedListener,android,text-to-speech,Android,Text To Speech,我想要的: 当TextToSpeech程序完成发言时,我希望收到通知,当它完成发言任务时,我希望执行一些GUI任务。 我所知道的: SetOnPatternanceCompletedListener可以用作回调,它应该会通知我它已经停止了讲话。它在API级别15中已经降低了价格,但仍然可以工作。 SetOnPatternanceProgressListener是另一种更好的方式,用于接收启动查找和错误通知的回调。实现此接口至少需要API级别15。我将我的最低sdk版本更改为15,并尝试了此方法,

我想要的:
当TextToSpeech程序完成发言时,我希望收到通知,当它完成发言任务时,我希望执行一些GUI任务。

我所知道的:

SetOnPatternanceCompletedListener可以用作回调,它应该会通知我它已经停止了讲话。它在API级别15中已经降低了价格,但仍然可以工作。

SetOnPatternanceProgressListener是另一种更好的方式,用于接收启动查找和错误通知的回调。实现此接口至少需要API级别15。我将我的最低sdk版本更改为15,并尝试了此方法,但它对我也不起作用。

这是我的代码


编辑:使用
处理程序
而不是
runOnUiThread()
方法


我建议您使用
onutternanceprogresslistener
,因为这个API有更多的方法来监听TextToSpeech的状态正在改变

但是,如果您想使用ONutternanceCompleted Listener,请按如下所示进行编辑:

 public class SpeakingNotepad extends AppCompatActivity  implements TextToSpeech.OnUtteranceCompletedListener{
    private final String UTTER_ID = "utterance";
    private final int TTS_DONE = 99;

    Button btnSpeak;
    EditText etText;
    TextToSpeech speaker;
    SpeechRecognizer voiceRecognizer;

    HashMap<String, String> ttsOptions;
    MainViewHandler g_hnd;

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

        g_hnd = new MainViewHandler(SpeakingNotepad.this);
        init();
    }

    private void init() {
        speaker = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                ttsOptions = new HashMap<String, String>();
                ttsOptions.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, UTTER_ID);

                speaker.setLanguage(Locale.ENGLISH);
            }
        });
        speaker.setOnUtteranceCompletedListener(this);
        btnSpeak = (Button) findViewById(R.id.btnSpeak);
        btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(etText.getText().length()>0)
//                    speaker.speak(etText.getText().toString(),TextToSpeech.QUEUE_FLUSH,null);
                    speaker.speak(etText.getText().toString(),TextToSpeech.QUEUE_FLUSH,ttsOptions);
            }
        });
        etText = (EditText) findViewById(R.id.etText);
    }

    @Override
    public void onUtteranceCompleted(String utteranceId) {
        if (utteranceId.equals(UTTER_ID)) {
            Log.d("textToSpeech", "completed");
            /*
            // for more common usage, use `Handler` instead of `runOnUiThread()`.
            // block
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(SpeakingNotepad.this, "completed", Toast.LENGTH_LONG).show();
                    btnSpeak.setBackgroundColor(Color.BLACK);
                }
            });
            */

            // then, you just call `sendMessage()`.
            g_hnd.sendMessage(TTS_DONE);
        }
    }

    private static class MainViewHandler extends Handler {
        private final WeakReference<SpeakingNotepad> mAct;

        MainViewHandler(SpeakingNotepad act) {
            mAct = new WeakReference<SpeakingNotepad>(act);
        }

        public void handleMessage(Message msg) {
            SpeakingNotepad act = mAct.get();
            if (act != null) {
                act.handleMessage(msg)
            }
        }
    }

    private void handleMessage(Message msg) {
        switch(msg.what) {
            case TTS_DONE:
                Toast.makeText(SpeakingNotepad.this, "completed", Toast.LENGTH_LONG).show();
                btnSpeak.setBackgroundColor(Color.BLACK);
                break;
        }
    }
}
公共类SpeakingNotepad扩展AppCompativeActivity实现TextToSpeech.OnPatternanceCompletedListener{
私有最终字符串outter_ID=“outrance”;
私人最终整数TTS_DONE=99;
按钮Btnpeak;
编辑文本;
TextToSpeech扬声器;
语音识别器;
HashMap-ttsOptions;
MainViewHandler g_hnd;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
g_hnd=新的MainViewHandler(SpeakingNotepad.this);
init();
}
私有void init(){
speaker=newtexttospeech(这是newtexttospeech.OnInitListener(){
@凌驾
公共无效onInit(int状态){
ttsOptions=newhashmap();
ttsOptions.put(TextToSpeech.Engine.KEY\u PARAM\u outrance\u ID,outter\u ID);
演讲者。设置语言(Locale.ENGLISH);
}
});
speaker.setonutternancecompletedlistener(this);
btnSpeak=(按钮)findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(etText.getText().length()>0)
//speaker.speak(etText.getText().toString(),TextToSpeech.QUEUE_FLUSH,null);
speaker.speak(etText.getText().toString(),TextToSpeech.QUEUE\u FLUSH,ttsOptions);
}
});
etText=(EditText)findViewById(R.id.etText);
}
@凌驾
公共void onutternancecompleted(字符串utternanceid){
if(outtanceId.equals(outter_ID)){
Log.d(“textToSpeech”,“completed”);
/*
//有关更常见的用法,请使用“Handler”而不是“runOnUiThread()”。
//挡块
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(SpeakingNotepad.this,“completed”,Toast.LENGTH_LONG.show();
Btnpeak.setBackgroundColor(颜色:黑色);
}
});
*/
//然后,您只需调用'sendMessage()`。
发送消息(TTS_DONE);
}
}
私有静态类MainViewHandler扩展了处理程序{
私人最终WeakReference mAct;
MainViewHandler(SpeakingNotepad法案){
mAct=新WeakReference(act);
}
公共无效handleMessage(消息消息消息){
SpeakingNotepad act=mAct.get();
if(act!=null){
手机短信(msg)
}
}
}
私有无效handleMessage(消息消息消息){
开关(msg.what){
案例TTS_完成:
Toast.makeText(SpeakingNotepad.this,“completed”,Toast.LENGTH_LONG.show();
Btnpeak.setBackgroundColor(颜色:黑色);
打破
}
}
}

编辑:使用
处理程序
而不是
runOnUiThread()
方法


我建议您使用
onutternanceprogresslistener
,因为这个API有更多的方法来监听TextToSpeech的状态正在改变

但是,如果您想使用ONutternanceCompleted Listener,请按如下所示进行编辑:

 public class SpeakingNotepad extends AppCompatActivity  implements TextToSpeech.OnUtteranceCompletedListener{
    private final String UTTER_ID = "utterance";
    private final int TTS_DONE = 99;

    Button btnSpeak;
    EditText etText;
    TextToSpeech speaker;
    SpeechRecognizer voiceRecognizer;

    HashMap<String, String> ttsOptions;
    MainViewHandler g_hnd;

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

        g_hnd = new MainViewHandler(SpeakingNotepad.this);
        init();
    }

    private void init() {
        speaker = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                ttsOptions = new HashMap<String, String>();
                ttsOptions.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, UTTER_ID);

                speaker.setLanguage(Locale.ENGLISH);
            }
        });
        speaker.setOnUtteranceCompletedListener(this);
        btnSpeak = (Button) findViewById(R.id.btnSpeak);
        btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(etText.getText().length()>0)
//                    speaker.speak(etText.getText().toString(),TextToSpeech.QUEUE_FLUSH,null);
                    speaker.speak(etText.getText().toString(),TextToSpeech.QUEUE_FLUSH,ttsOptions);
            }
        });
        etText = (EditText) findViewById(R.id.etText);
    }

    @Override
    public void onUtteranceCompleted(String utteranceId) {
        if (utteranceId.equals(UTTER_ID)) {
            Log.d("textToSpeech", "completed");
            /*
            // for more common usage, use `Handler` instead of `runOnUiThread()`.
            // block
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(SpeakingNotepad.this, "completed", Toast.LENGTH_LONG).show();
                    btnSpeak.setBackgroundColor(Color.BLACK);
                }
            });
            */

            // then, you just call `sendMessage()`.
            g_hnd.sendMessage(TTS_DONE);
        }
    }

    private static class MainViewHandler extends Handler {
        private final WeakReference<SpeakingNotepad> mAct;

        MainViewHandler(SpeakingNotepad act) {
            mAct = new WeakReference<SpeakingNotepad>(act);
        }

        public void handleMessage(Message msg) {
            SpeakingNotepad act = mAct.get();
            if (act != null) {
                act.handleMessage(msg)
            }
        }
    }

    private void handleMessage(Message msg) {
        switch(msg.what) {
            case TTS_DONE:
                Toast.makeText(SpeakingNotepad.this, "completed", Toast.LENGTH_LONG).show();
                btnSpeak.setBackgroundColor(Color.BLACK);
                break;
        }
    }
}
公共类SpeakingNotepad扩展AppCompativeActivity实现TextToSpeech.OnPatternanceCompletedListener{
私有最终字符串outter_ID=“outrance”;
私人最终整数TTS_DONE=99;
按钮Btnpeak;
编辑文本;
TextToSpeech扬声器;
语音识别器;
HashMap-ttsOptions;
MainViewHandler g_hnd;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
g_hnd=新的MainViewHandler(SpeakingNotepad.this);
init();
}
私有void init(){
speaker=newtexttospeech(这是newtexttospeech.OnInitListener(){
@凌驾
公共无效onInit(int状态){
ttsOptions=newhashmap();
ttsOptions.put(TextToSpeech.Engine.KEY\u PARAM\u outrance\u ID,outter\u ID);
演讲者。设置语言(Locale.ENGLISH);
}
});
speaker.setonutternancecompletedlistener(this);
btnSpeak=(按钮)findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(etText.getText().length()>0)
//speaker.speak(etText.getText().toString(),TextToSpeech.QUEUE_FLUSH,null);
speaker.speak(etText.getText().toString(),TextToSpeech.QUEUE\u FLUSH,ttsOptions);
}
});
etText=(EditText)findViewById(R.id.etText);
}
@凌驾
已完成公共无效操作