2012-01-05 41 views
0

我有文字到语音全部在这里设置(或者我认为),但当我按下我的应用程序上的按钮它只是关闭,并没有说我输入什么,(现在我想知道是不是因为我没有设置OnClickListener事件,原因是我真的知道如何使用TextToSpeach设置OnClickListener事件,所以如果这是我的应用程序不说话的原因,那么可以有人指点我在这个领域,这将有助于教程。如果它不是原因,那么有人可以解释我做错了吗?我在做什么错TextToSpeech

package com.write.it; 

    import java.util.HashMap; 
    import java.util.StringTokenizer; 

    import android.app.Activity; 
    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.widget.Button; 
    import android.widget.EditText; 

    public class Speech extends Activity 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 = "TTS Demo"; 
     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); 

     // 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 doSpeak(View view) { 
    StringTokenizer st = new StringTokenizer(words.getText().toString(),",."); 
    while (st.hasMoreTokens()) { 
     params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, 
       String.valueOf(uttCount++)); 
     mTts.speak(st.nextToken(), TextToSpeech.QUEUE_ADD, params); 
    } 
    } 

    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) { 
     speakBtn.setEnabled(true); 
     mTts.setOnUtteranceCompletedListener(this); 
    } 
} 

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

新代码

package com.write.it; 

    import java.util.HashMap; 
    import java.util.StringTokenizer; 

    import android.app.Activity; 
    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.widget.Button; 
    import android.widget.EditText; 

    public class Speech extends Activity 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 = "TTS Demo"; 
    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); 
    mTts = new TextToSpeech(this, 
      this); // TextToSpeech.OnInitListener 


     // 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 doSpeak(View view) { 
    StringTokenizer st = new StringTokenizer(words.getText().toString(),",."); 
    while (st.hasMoreTokens()) { 
     params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, 
       String.valueOf(uttCount++)); 
     mTts.speak(st.nextToken(), TextToSpeech.QUEUE_ADD, params); 
    } 
    speakBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // /TODO Auto-generated method stub 
      doSpeak(v); 

     } 
    }); 

     } 

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

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


    } 

回答

0

您在doSpeak()方法中编写TTS说法。而且你甚至不称这种方法为doSpeak()。那你如何期待说出来?

在发言按钮中设置一个监听器,并在监听器中调用doSpeak()。如果它仍然不说话,让我知道。

+0

ok我试试看,谢谢你,这是我第一次做textToSpeech,所以我完全不确定一切 – 2012-01-05 07:35:37

+0

我称之为它仍然没有工作 – 2012-01-05 08:02:20

+0

我每次按下我的按钮时都会关闭 – 2012-01-05 08:05:02