2010-12-21 16 views
1

Ich bin neu in Android-Entwicklung, Eclipse und Java (bisher meist .Net und IVR-Programmierung), also als ich versuchte, eine Beispiel-App für TTS zu kompilieren und auszuführen Beim Droid war ich nicht überrascht, dass ich sofort einen Laufzeitfehler bekam. Der Fehler lautet:Fehler bei der Auflösung der TextToSpeech-Klasse für Android-Entwicklung

dalvikvm Fehler bei der Auflösung com/sample/TTSapp/AndroidTTSapp; Schnittstelle 4 'android/Sprache/tts/TextToSpeech $ OnInitListner;'

nehme ich die OnInitListner Methode in einer der Klassen sein müssen, die installiert wurde, wenn ich das Android SDK installiert (Version 1.6 R1 glaube ich), aber ich bin nicht sicher, wie das zugehörige Klassenmodul in das importieren aktuelles Programm. Ich kann nirgendwo auf meinem System ein Verzeichnis "sprache/tts/TextToSpeech" finden. Muss ich dieses Verzeichnis von irgendwo herunterladen? Im Anschluss ist der Java-Quellcode für das Demo TTS Programm Ich versuche zu laufen:


Paket com.sample.TTSApp;

importieren android.app.Activity; importieren android.os.Bundle; importieren android.speech.tts.TextToSpeech; importieren android.util.Log; importieren android.view.View; importieren android.widget.Button; import com.sample.TTSApp.R; Import java.util.Locale; importieren java.util.Random; Öffentliche Klasse AndroidTTSapp erweitert Aktivität implementiert TextToSpeech.OnInitListener { private statische letzte Zeichenfolge TAG = "TextToSpeechDemo";
private TextToSpeech mTts; private Schaltfläche mAgainButton; @Override public void onCreate (Paket savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.main); // Initialisiere Text-zu-Sprache. Dies ist eine asynchrone Operation.
// Der OnInitListener (zweites Argument) wird nach Abschluss der Initialisierung aufgerufen. // Instantiate TextToSpeech.OnInitListener
mTts = neue TextToSpeech (this, this); mAgainButton = (Schaltfläche) findViewById (R.id.again_button);

mAgainButton.setOnClickListener(new View.OnClickListener() 
     { 
    public void onClick(View v) 
    { 
    sayHello(); 
     } 
     }); 
    } 
@Override 
public void onDestroy() 
    {  // Don't forget to shutdown! 
    if (mTts != null) 
     { 
     mTts.stop(); 
     mTts.shutdown(); 
     } 
    super.onDestroy(); 

} // Implementiert TextToSpeech.OnInitListener.

public void onInit(int status) 
    { 
    // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR. 
    if (status == TextToSpeech.SUCCESS) 
    {  
     int result = mTts.setLanguage(Locale.US); 
     if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) 
     { 
      // Language data is missing or the language is not supported. 
      Log.e(TAG, "Language is not available."); 
     } 
    else 
    { 
     // Check the documentation for other possible result codes. For example, the language may be available for the locale 
    // but not for the specified country and variant.  
     // The TTS engine has been successfully initialized. Allow the user to press the button for the app to speak again. 
     mAgainButton.setEnabled(true); 
    // Greet the user 
     sayHello(); 
     }    

} sonst { // Initialisierung fehlgeschlagen. Log.e (TAG, "konnte TextToSpeech nicht initialisieren."); } };

private static final Random RANDOM = new Random(); 
private static final String[] HELLOS = 
{ 
    "Hello World", "This is Text to speech demo by Zahid Shaikh" 
}; 

private void sayHello() 
{ 
// Select a random hello. 
int i =0; 
int helloLength = HELLOS.length; 
String hello = HELLOS[i]; 
i++; 
if(i == helloLength) i =0; 
mTts.speak(hello,TextToSpeech.QUEUE_FLUSH,null); 
} 

}

Vielen Dank im Voraus für jede assitance jeder kann einen Anfänger wie mich geben.

Don Tilley

Antwort

0

Auf Gerät oder Emulator in */system/tts/lang_pico/* muss tts-lang-Dateien (* .bin) sein.

Es ist init TTS Beispiel:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ........ 
    ........ 
    initTTS(); 
} 

private void initTTS() { 
    Intent checkIntent = new Intent(); 
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
    startActivityForResult(checkIntent, MY_DATA_CHECK_CODE); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
if(requestCode == MY_DATA_CHECK_CODE) { 
    if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { 
    mTts = new TextToSpeech(this, this); 
    } else { 
    Intent installIntent = new Intent(); 
    installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 
    startActivity(installIntent); 
     } 
    } 
} 
public void onInit(int status) { 
    if(status == TextToSpeech.SUCCESS) { 
     int result = mTts.setLanguage(Locale.US); 
       if(result == TextToSpeech.LANG_AVAILABLE 
        || result == TextToSpeech.LANG_COUNTRY_AVAILABLE) { 
         mTts.speak("Start system", TextToSpeech.QUEUE_FLUSH, null); 
       } 
    } 
} 
Verwandte Themen