2012-01-08 67 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

相关问题