无法启动TTS(文本到语音)

时间:2012-01-08 21:43:38

标签: android

好吧基本上我已经完成了所有设置并且我已多次设置我的onClick方法,但这一次是在我的main.xml文件中设置的

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



}

2 个答案:

答案 0 :(得分:1)

您的代码中混合了很多内容,尤其是您使用StartActivityforResult()检查引擎的部分。

请参阅此TTS工作示例:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/TextToSpeechActivity.html

此外,您没有为按钮指定任何ID(显然?)可能是,另一个按钮正在使用该ID?

<强> /编辑: 首先,合并两个活动。如果再次拨打onCreate(),它将覆盖前一个。然后添加implements OnClickListener并替换

speakBtn.setOnClickListener((OnClickListener) this);

speakBtn.setOnClickListener(this);

Eclipse将告诉您需要添加未实现的方法。

答案 1 :(得分:0)

如果您在问题顶部复制的XML代码是您必须声明该按钮的全部内容,那么它在处理程序中缺少您期望的ID(R.id.speak)...

我建议你在doButton函数中添加一些调试打印,看看是否正确调用它,然后再进行文本转语音等等。