Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

javascript - Google's Text-To-Speech API from Android app

I want to use Google's Text-To-Speech API in an Android app but I only could find the way to do it from web (Chrome). This is my first attempt to play "Hello world" from the app.

playTTS is the onClick and it is been executed, but no sound is played. Is there any JS/Java library I need to import? Is it possible to generate an audio file from it?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myBrowser = new WebView(this);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

public void playTTS(View view) {
    myBrowser.loadUrl("javascript:speechSynthesis.speak(
         SpeechSynthesisUtterance('Hello World'))");
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Android java code your Activity/other Class should implement TextToSpeech.OnInitListener. You will get a TextToSpeech instance by calling TextToSpeech(context, this). (Where context refers to your application's Context -- can be this in an Activity.) You will then receive a onInit() callback with status which tells whether the TTS engine is available or not.

You can talk by calling tts.speak(textToBeSpoken, TextToSpeech.QUEUE_FLUSH, null) or tts.speak(textToBeSpoken, TextToSpeech.QUEUE_ADD, null). The first one will interrupt any "utterance" that is still being spoken and the latter one will add the new "utterance" to a queue. The last parameter is not mandatory. It could be an "utterance id" defined by you in case you want to monitor the TTS status by setting an UtteranceProgressListener. (Not necessary)

In Java code a simple "TTS talker" class could be something like:

public class MyTtsTalker implements TextToSpeech.OnInitListener {

  private TextToSpeech tts;
  private boolean ttsOk;

  // The constructor will create a TextToSpeech instance.
  MyTtsTalker(Context context) {
    tts = new TextToSpeech(context, this);
  }

  @Override
  // OnInitListener method to receive the TTS engine status
  public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
      ttsOk = true;
    }
    else {
      ttsOk = false;
    }
  }

  // A method to speak something
  @SuppressWarnings("deprecation") // Support older API levels too.
  public void speak(String text, Boolean override) {
    if (ttsOk) {
      if (override) {
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);    
      }    
      else {
        tts.speak(text, TextToSpeech.QUEUE_ADD, null);    
      }
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...