我的文字转语音功能不想跟我顶嘴

发布于 2024-12-24 22:46:37 字数 4043 浏览 1 评论 0原文

我的 TextToSpeech android 仍然有问题,我为实现的语音(如 exlipse 也问我)输入了两次按钮,为实现的 OnClickListener 输入了一次按钮,即使我取出按钮的底部操作,它也不想播放, 为什么?我不知道它只是不想说出我的 EditText 字段中的内容,我不知道为什么它不想,而且我已经问过很多次了,但我还没有得到有效的答案。

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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Speech extends Activity implements OnInitListener, OnUtteranceCompletedListener, OnClickListener {
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);
     }

    speakBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.speak:
                doSpeak(v);
                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 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);
        speakBtn.setEnabled(true);
    }
}

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

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

I am still having problems with my TextToSpeech android, I entered the button twice once for the implemented Speech like exlipse asked me too and one for the implemented OnClickListener, Even if I take out the bottom action for the button it doesn't want to play, Why? idk it just doesn't want to say what is in my EditText Field I have no clue why it doesn't want to and I have asked around many times but I haven't goten an answer that worked.

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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Speech extends Activity implements OnInitListener, OnUtteranceCompletedListener, OnClickListener {
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);
     }

    speakBtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.speak:
                doSpeak(v);
                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 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);
        speakBtn.setEnabled(true);
    }
}

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-12-31 22:46:37

您是否尝试过仅通过一种方法调用来朗读文本?像这样

mTts.speak(aString, TextToSpeech.QUEUE_ADD, null);

它对我来说效果很好。我使用的初始化和你的一样。唯一剩下的区别是(这不是让 TTS 工作所必需的!):

        this.speaker.setSpeechRate(0.9f);
        this.speaker.setPitch(0.9f);

Did you try speaking the text with just one method call? Like this

mTts.speak(aString, TextToSpeech.QUEUE_ADD, null);

It works fine for me. The initialization I use is the same as yours. The only remaining difference is this (which should not be necessary to get the TTS to work!):

        this.speaker.setSpeechRate(0.9f);
        this.speaker.setPitch(0.9f);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文