2010-12-21 55 views
1

我是Android開發,Eclipse和Java的新手(迄今爲止主要完成.Net和IVR編程),所以當我試圖編譯和運行我發現的TTS示例應用程序在Droid上,我並不感到驚訝,我馬上得到了運行時錯誤。錯誤是:未解決Android開發的TextToSpeech類

dalvikvm無法解析com/sample/TTSapp/AndroidTTSapp;界面4'android/speech/tts/TextToSpeech $ OnInitListner;'

我想OnInitListner方法必須在我安裝Android SDK時安裝的一個類中(我相信1.6版R1),但我不確定如何將關聯的類模塊導入到目前的計劃。我無法在系統中的任何位置找到語音/ tts/TextToSpeech目錄。我需要從某處下載此目錄嗎?以下是我嘗試運行的演示TTS程序的Java源代碼:


package com.sample.TTSApp;

import android.app.Activity; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.util.Log; import android.view.View; import android.widget.Button; import com.sample.TTSApp.R; import java.util.Locale; import java.util.Random; public class AndroidTTSapp extends Activity implements TextToSpeech.OnInitListener private static final String TAG =「TextToSpeechDemo」;
private TextToSpeech mTts; 私人按鈕mAgainButton; (保存實例狀態) @Override setContentView(R.layout.main); //初始化文本到語音轉換。這是一個異步操作。
//初始化完成後調用OnInitListener(第二個參數)。 //實例化TextToSpeech.OnInitListener
mTts = new TextToSpeech(this,this); mAgainButton =(Button)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(); 

} // 實現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(); 
     }    

} 其他 {// 初始化失敗。 Log.e(TAG,「無法初始化TextToSpeech。」); } };

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); 
} 

}

預先感謝任何assitance誰能給初學者喜歡自己。

唐特利

回答

0

在設備或仿真器中* /系統/ TTS/lang_pico/*必須TTS琅文件(* BIN)。

它的init TTS例如:

@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); 
       } 
    } 
}