Android 中的 tts 问题(服务未启动)

发布于 2024-12-08 11:54:12 字数 2485 浏览 1 评论 0原文

我是 android 初学者,我在 android 中遇到 tts 问题,eclipse 在日志中写入:

10-04 15:27:49.851:错误/TextToSpeech.java - 说话(19515):服务 尚未启动

这是我的代码,全部来自示例:

package ru.andr.dictu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;

/**
 * This class demonstrates checking for a TTS engine, and if one is
 * available it will spit out some speak based on what is in the
 * text field.
 */
public class speak extends Activity implements TextToSpeech.OnInitListener
{
    public static String text = "some text";
    private TextToSpeech mTts;
    // This code can be any value you want, its just a checksum.
    private static final int MY_DATA_CHECK_CODE = 1234;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.speech);
        // Fire off an intent to check if a TTS engine is installed
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
    }
    public void speakClicked(View v)
    {

    }
    public void onInit(int i)
    {

    }
   public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == MY_DATA_CHECK_CODE)
        {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
            {
                // success, create the TTS instance
                mTts = new TextToSpeech(this, this);
                //try to speak
                LetsSpeak();
            }
            else
            {
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent.setAction(
                        TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }
    }
    @Override
    public void onDestroy()
    {
        // Don't forget to shutdown!
        if (mTts != null)
        {
            mTts.stop();
            mTts.shutdown();
        }
        super.onDestroy();
    }

    public void LetsSpeak() {
        // TODO Auto-generated method stub

        mTts.speak(text,
                TextToSpeech.QUEUE_FLUSH,  // Drop all pending entries in the playback queue.
                null);
    }
}

I am beginner in android and I have an issue with tts in android, eclipse writes in log:

10-04 15:27:49.851: ERROR/TextToSpeech.java - speak(19515): service
isn't started

There is my code, all from example:

package ru.andr.dictu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;

/**
 * This class demonstrates checking for a TTS engine, and if one is
 * available it will spit out some speak based on what is in the
 * text field.
 */
public class speak extends Activity implements TextToSpeech.OnInitListener
{
    public static String text = "some text";
    private TextToSpeech mTts;
    // This code can be any value you want, its just a checksum.
    private static final int MY_DATA_CHECK_CODE = 1234;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.speech);
        // Fire off an intent to check if a TTS engine is installed
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
    }
    public void speakClicked(View v)
    {

    }
    public void onInit(int i)
    {

    }
   public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == MY_DATA_CHECK_CODE)
        {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
            {
                // success, create the TTS instance
                mTts = new TextToSpeech(this, this);
                //try to speak
                LetsSpeak();
            }
            else
            {
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent.setAction(
                        TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }
    }
    @Override
    public void onDestroy()
    {
        // Don't forget to shutdown!
        if (mTts != null)
        {
            mTts.stop();
            mTts.shutdown();
        }
        super.onDestroy();
    }

    public void LetsSpeak() {
        // TODO Auto-generated method stub

        mTts.speak(text,
                TextToSpeech.QUEUE_FLUSH,  // Drop all pending entries in the playback queue.
                null);
    }
}

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

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

发布评论

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

评论(1

止于盛夏 2024-12-15 11:54:12

好的,更改了类名 - 问题解决了。

和:

    public void onInit(int i)
{
    if (i==mTts.SUCCESS)
    {
        mySayFunc();
        mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener(){

            @Override
            public void onUtteranceCompleted(String utId) {
                // TODO Auto-generated method stub
                if (utId.indexOf("ok")!=-1)
                finish();
            }

        });
    }
}

Ok, changed class name - problem was solved.

and:

    public void onInit(int i)
{
    if (i==mTts.SUCCESS)
    {
        mySayFunc();
        mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener(){

            @Override
            public void onUtteranceCompleted(String utId) {
                // TODO Auto-generated method stub
                if (utId.indexOf("ok")!=-1)
                finish();
            }

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