2013-08-08 10 views
9

Così ho avuto un'attività originale con praticamente lo stesso esatto codice per parlare, ma ho dovuto spostare quel codice in un'altra attività. L'unica differenza che posso dire è che il text to speech non viene chiamato in un metodo asincrono. Il parlare avviene nel metodo speakFull. ottengo questi errori:Speak Failed Non associato a TTS Engine

speak failed: not bound to TTS engine 
isSpeaking failed: not bound to TTS engine 

Sono nuovo di sviluppo Android, ho cercato attraverso altre soluzioni a questo problema, e non posso davvero sembrano trovare una soluzione per far funzionare la mia. Qualsiasi consiglio o aiuto è apprezzato.

Codice:

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 

    } 

} 

risposta

12

È possibile chiamare solo speak() dopo onInit() è stato chiamato. Così si muovono i tuoi TTS parlano codice onCreate a onInit()

@Override 
public void onInit(int status) { 
    if (status == TextToSpeech.SUCCESS) { 
     leftToRead = speakFull(res); 

} 

e inizializzare pausa true boolean paused = true;

+0

Questo funziona, ho una domanda se ora. A volte cambierà per mostrare la seconda attività, e tutta l'interfaccia utente ea volte andrà a una schermata nera e inizierà a leggere, e Android ti dirà costantemente che il programma non risponde. Sapresti perché? –

+0

Puoi riformulare e correggere la grammatica e l'ortografia nel tuo commento perché non capisco la tua domanda. –

+0

Ha funzionato anche per me. Grazie! –