java– 说话失败没有绑定到TTS引擎

java– 说话失败没有绑定到TTS引擎,第1张

概述所以我有一个原始活动与基本相同的说话代码,但我不得不将该代码移动到另一个活动.我能说的唯一区别是文本到语音不是在异步方法中调用的.说话发生在speakFull方法中.我收到这些错误:speakfailed:notboundtoTTSengineisSpeakingfailed:notboundtoTTSengine我是Andr

所以我有一个原始活动与基本相同的说话代码,但我不得不将该代码移动到另一个活动.我能说的唯一区别是文本到语音不是在异步方法中调用的.说话发生在speakFull方法中.
我收到这些错误:

speak Failed: not bound to TTS engineisspeaking Failed: not bound to TTS engine

我是AndroID开发的新手,我已经通过其他解决方案搜索了这个问题,我似乎无法找到一个解决方案让我的工作.任何建议或帮助表示赞赏.

码:

   package com.example.webvIEw;import androID.os.Bundle;import androID.app.Activity;import androID.content.Context;import androID.content.DialogInterface;import androID.content.DialogInterface.OnClickListener;import androID.content.Intent;import androID.speech.tts.TextToSpeech;import androID.text.method.ScrollingMovementMethod;import androID.vIEw.Menu;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.TextVIEw;public class ReadOut extends Activity implements TextToSpeech.OnInitListener, OnClickListener {    boolean paused = false;    String leftToRead = null;    String res = null;    @SuppressWarnings("deprecation")    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.read_out);        Intent intent = getIntent();        res = intent.getExtras().getString("response");        TextVIEw textv = (TextVIEw) findVIEwByID(R.ID.textVIEw1);        textv.setText(res);        textv.setMovementMethod(new ScrollingMovementMethod());        androID.vIEw.display display = ((androID.vIEw.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultdisplay();         textv.setHeight((int)(display.getHeight()*0.76));        leftToRead = speakFull(res);    }    @OverrIDe    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        return true;    }    public String speakFull(String text){        System.out.println("Speaking: " + text);        TextToSpeech tts = new TextToSpeech(this, this);        System.out.println("Speaking");        String[] sentences = text.split("\n|\.(?!\d)|(?<!\d)\."); // Regex that splits the body of text into the sentences of that body which are stored in a String array.        for(int i = 0; i < sentences.length; i++){            if(!tts.isspeaking() && !paused){                System.out.println("Speaking: " + i);                tts.speak(sentences[i], TextToSpeech.QUEUE_FLUSH, null);            }else if(paused){                System.out.println("Paused");                String paused = "";                for(int j = i - 1; j < sentences.length; j++){                    paused += sentences[j];                }                return paused;            }else{                i--;            }            if(i == sentences.length - 1){                return "Message 001: Complete";            }        }        return null;    }    @OverrIDe    public voID onInit(int arg0) {        // Todo auto-generated method stub    }    public voID clickPause(VIEw v){        if(paused){            paused = false;            button b = (button) findVIEwByID(R.ID.button1);            b.setText("Play");        }else{            paused = true;            button b = (button) findVIEwByID(R.ID.button1);            b.setText("Pause");            if(leftToRead == null){                leftToRead = speakFull(res);            }else{                leftToRead = speakFull(leftToRead);            }        }    }    @OverrIDe    public voID onClick(DialogInterface arg0, int arg1) {        // Todo auto-generated method stub    }}

解决方法:

你只能在调用onInit()后调用speak().所以移动你的tts说onCreate中的代码到onInit()

@OverrIDepublic voID onInit(int status) {    if (status == TextToSpeech.SUCCESS) {         leftToRead = speakFull(res);}

并将pause初始化为true boolean paused = true;

总结

以上是内存溢出为你收集整理的java – 说话失败没有绑定到TTS引擎全部内容,希望文章能够帮你解决java – 说话失败没有绑定到TTS引擎所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1102497.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存