2012-01-08 116 views
1

好的基本上我把一切都建立了,我已經建立了我的onClick方法很多次,但是這次它是建立在我的main.xml文件無法啓動TTS(文本到語音)

<Button 
    android:layout_alignParentRight="true" 
    android:layout_height="40dip" 
    android:layout_width="80dip" 
    android:id="@+id/speak" 
    android:enabled="true" 
    android:visible="true" 
    android:text="Speak" > 

</Button> 

明顯它有其他的東西來定義它的佈局,但這是我的按鈕內的xml佈局之一,並且由於某種原因,每次我點擊按鈕時,它都會關閉整個應用程序,爲什麼? 也如果我設置它來添加一個onClickListener然後該按鈕只是什麼都不做。

我會成立我的onClick監聽器這樣

speak.setOnClickListener(this); 
    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.speak: 
     doSpeak(); 
     break; 
    } 
} 

,它仍然不工作....所以我只是不知道我做錯了什麼

 package com.write.it; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.media.MediaPlayer; 
    import android.os.Bundle; 
    import android.speech.tts.TextToSpeech; 
    import android.speech.tts.TextToSpeech.OnInitListener; 
    import android.util.Log; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.EditText; 

    public class Speech extends Activity implements OnInitListener { 
private EditText words = null; 
private Button speakBtn = null; 
private static final int REQ_TTS_STATUS_CHECK = 0; 
private static final String TAG = "TTS Demo"; 
    private TextToSpeech mTts = null; 
    private MediaPlayer player = null; 

/** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    words = (EditText)findViewById(R.id.wordsToSpeak); 
    speakBtn = (Button)findViewById(R.id.speak); 



    // Check to be sure that TTS exists and is okay to use 
    Intent checkIntent = new Intent(); 
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
    startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK); 
} 

public void doButton(View view) { 
    switch(view.getId()) { 
    case R.id.speak: 
     mTts.speak(words.getText().toString(), TextToSpeech.QUEUE_ADD, null); 
     break; 

     } 
      } 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQ_TTS_STATUS_CHECK) { 
     switch (resultCode) { 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS: 
      // TTS is up and running 
      mTts = new TextToSpeech(this, this); 
      Log.v(TAG, "Pico is installed okay"); 
      break; 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA: 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA: 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME: 
      // missing data, install it 
      Log.v(TAG, "Need language stuff: " + resultCode); 
      Intent installIntent = new Intent(); 
      installIntent.setAction(
       TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 
      startActivity(installIntent); 
      break; 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL: 
     default: 
      Log.e(TAG, "Got a failure. TTS apparently not available"); 
     } 
    } 
    else { 
     // Got something else 
    } 
} 

public void onInit(int status) { 
    // Now that the TTS engine is ready, we enable buttons 
    if(status == TextToSpeech.SUCCESS) { 
     speakBtn.setEnabled(true); 
     } 
} 

@Override 
public void onPause() 
{ 
    super.onPause(); 
    // if we're losing focus, stop playing 
    if(player != null) { 
     player.stop(); 
    } 
    // if we're losing focus, stop talking 
    if(mTts != null) 
     mTts.stop(); 
} 

@Override 
public void onDestroy() 
{ 
    super.onDestroy(); 
    if(player != null) { 
     player.release(); 
    } 
    if(mTts != null) { 
     mTts.shutdown(); 
    } 
     } 
    } 

我的意思是我沒有設置音量或設置或讓它說話?我不告訴它做它應該做的事情?.....我的手機上的音量開啓,媒體音量也開始上升,我有一個HTC evo 4G所以它應該工作..... 在這裏我添加另一個話語和onClick方法

package com.write.it; 


    import java.util.HashMap; 
    import java.util.Locale; 
    import java.util.StringTokenizer; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.speech.tts.TextToSpeech; 
    import android.speech.tts.TextToSpeech.OnInitListener; 
    import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener; 
    import android.util.Log; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.EditText; 


    public class Speech extends WriteitActivity implements OnInitListener, OnUtteranceCompletedListener { 
private EditText words = null; 
private Button speakBtn = null; 
    private static final int REQ_TTS_STATUS_CHECK = 0; 
private static final String TAG = "Word Guesser"; 
    private TextToSpeech mTts; 

    private int uttCount = 0; 
    private HashMap<String, String> params = new HashMap<String, String>(); 

/** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    words = (EditText)findViewById(R.id.wordsToSpeak); 
    speakBtn = (Button)findViewById(R.id.speak); 
    speakBtn.setOnClickListener((OnClickListener) this); 
    mTts.isLanguageAvailable(Locale.US); 



    // Check to be sure that TTS exists and is okay to use 
    Intent checkIntent = new Intent(); 
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
    startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK); 
    } 

    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.speak: 
     doSpeak(); 
     break; 
    } 
} 

public void doSpeak() { 
    StringTokenizer st = new StringTokenizer(words.getText().toString(),",."); 
    while (st.hasMoreTokens()) { 
     params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, 
       String.valueOf(uttCount++)); 
     mTts.speak(getString(R.id.wordsToSpeak), TextToSpeech.QUEUE_ADD, null); 
     mTts.speak(getString(R.id.wordsToSpeak), TextToSpeech.SUCCESS, null); 

    } 



} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQ_TTS_STATUS_CHECK) { 
     switch (resultCode) { 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS: 
      // TTS is up and running 
      mTts = new TextToSpeech(this, this); 
      Log.v(TAG, "Pico is installed okay"); 
      break; 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA: 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA: 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME: 
      // missing data, install it 
      Log.v(TAG, "Need language stuff: " + resultCode); 
      Intent installIntent = new Intent(); 
      installIntent.setAction(
       TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 
      startActivity(installIntent); 
      break; 
     case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL: 
     default: 
      Log.e(TAG, "Got a failure. TTS not available"); 
     } 
    } 
    else { 
     // Got something else 
    } 
} 

public void onInit(int status) { 
    // Now that the TTS engine is ready, we enable the button 
    if(status == TextToSpeech.SUCCESS) { 
     mTts.setOnUtteranceCompletedListener(this); 
} 
} 

public void onPause() 
{ 
    super.onPause(); 
    // if we're losing focus, stop talking 
    if(mTts != null) 
     mTts.stop(); 
} 

@Override 
public void onDestroy() 
{ 
    super.onDestroy(); 
    mTts.shutdown(); 
} 

public void onUtteranceCompleted(String uttId) { 
    Log.v(TAG, "Got completed message for uttId: " + uttId); 
    Integer.parseInt(uttId); 
} 



} 

回答

1

代碼中有很多東西混淆在一起,特別是用StartActivityforResult()檢查引擎的部分。

請參閱工作TTS的這個例子:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/TextToSpeechActivity.html

你也沒有指派任何標識的按鈕(?顯然)莫非,另一個按鈕使用ID?

/編輯: 首先,合併兩個活動。如果您再次撥打onCreate(),則會覆蓋前一個。然後加入implements OnClickListener

speakBtn.setOnClickListener(this); 

Eclipse會告訴你,你需要添加一個未實現的方法代替

speakBtn.setOnClickListener((OnClickListener) this); 

+0

沒有另一個按鈕沒有使用ID,但我會看到我可以用StartActivityResult()做什麼 – 2012-01-08 22:14:36

+0

請發佈佈局xml,因爲它看起來像有更多的一個按鈕,我想這會有很大的幫助。 – Force 2012-01-08 22:17:10

+0

我沒有分配一個id其android:@ + id/speak – 2012-01-08 22:18:59

0

如果你在你的問題上覆制的XML代碼是你必須聲明,按鈕,然後它缺少在處理你所期望的ID(R.id.speak)...

我建議你在doButton函數中添加一些調試打印,看看在文本到語音轉換等任何進一步之前,它是否被正確調用。

+0

我剛剛在xml佈局中取出了anroid:onClick方法 – 2012-01-08 22:22:27