本文介绍了文本到语音服务未在android系统启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做出这样的应用程序时,图像加载它的自我自动说话。
我曾尝试它工作正常TTS的单个应用程序(例如),但这code给我
我的code为文本到语音服务未启动。

 公共类show_image扩展活动实现TextToSpeech.OnInitListener {意图mIntent;
字符串值;
ImageView的mImageView;
私人文字转语音TTS;
诠释J;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    mIntent = getIntent();
    束B = getIntent()getExtras()。
    最终的字符串名称= b.getString(名称);
    J = name.toCharArray()[0];    TTS =新的文字转语音(这一点,这一点);
    tts.speak(欢迎光临,TextToSpeech.QUEUE_ADD,NULL);
    mImageView =(ImageView的)findViewById(R.id.imageView1);    开关(J){
    案例65:
        mImageView.setBackgroundResource(R.drawable.a);
        tts.speak(名称+苹果,TextToSpeech.QUEUE_ADD,NULL);
        // Toast.makeText(getApplicationContext(),
        //云:+姓名+苹果,Toast.LENGTH_SHORT)
        // 。显示();
        tts.isLanguageAvailable(Locale.ENGLISH);
        打破;
    案例66:
        mImageView.setBackgroundResource(R.drawable.b);
        tts.speak(名称+蓝牙,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例67:
        mImageView.setBackgroundResource(R.drawable.c);
        tts.speak(名称+的聊天室,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例68:
        mImageView.setBackgroundResource(R.drawable.d);
        tts.speak(名称+的下载,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例69:
        mImageView.setBackgroundResource(R.drawable.e);
        tts.speak(名称+对于E邮报,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例70:
        mImageView.setBackgroundResource(R.drawable.f);
        tts.speak(名称+对于Facebook,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例71:
        mImageView.setBackgroundResource(R.drawable.g);
        tts.speak(名称+为谷歌,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例72:
        mImageView.setBackgroundResource(R.drawable.h);
        tts.speak(名称+为Hewlwtt惠普,TextToSpeech.QUEUE_ADD,
                空值);
        打破;
    案例73:
        mImageView.setBackgroundResource(R.drawable.i);
        tts.speak(名称+为iPhone,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例74:
        mImageView.setBackgroundResource(R.drawable.j);
        tts.speak(名称+的Java,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例75:
        mImageView.setBackgroundResource(R.drawable.k);
        tts.speak(名称+金士顿,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例76:
        mImageView.setBackgroundResource(R.drawable.l);
        tts.speak(名称+笔记本,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例77:
        mImageView.setBackgroundResource(R.drawable.m);
        tts.speak(名称+的信使,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例78:
        mImageView.setBackgroundResource(R.drawable.n);
        tts.speak(名称+的尼禄,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例79:
        mImageView.setBackgroundResource(R.drawable.o);
        tts.speak(名称+为Orkut的,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例80:
        mImageView.setBackgroundResource(R.drawable.p);
        tts.speak(名称+为Picassa中,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例81:
        mImageView.setBackgroundResource(R.drawable.q);
        tts.speak(名称+为快速治愈,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例82:
        mImageView.setBackgroundResource(R.drawable.r);
        tts.speak(名称+对于拉姆,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例83:
        mImageView.setBackgroundResource(R.drawable.s);
        tts.speak(名称+的服务器,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例84:
        mImageView.setBackgroundResource(R.drawable.t);
        tts.speak(名称+的微博,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例85:
        mImageView.setBackgroundResource(R.drawable.u);
        tts.speak(名称+对于USB,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例86:
        mImageView.setBackgroundResource(R.drawable.v);
        tts.speak(名称+为Vista,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例87:
        mImageView.setBackgroundResource(R.drawable.w);
        tts.speak(名称+支持WiFi,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例88:
        mImageView.setBackgroundResource(R.drawable.x);
        tts.speak(名称+为XP,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例89:
        mImageView.setBackgroundResource(R.drawable.y);
        tts.speak(名称+For YouTube的,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    案例90:
        mImageView.setBackgroundResource(R.drawable.z);
        tts.speak(名称+的若比邻,TextToSpeech.QUEUE_ADD,NULL);
        打破;
    默认:
        打破;
    }    意图checkIntent =新的Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent,RESULT_OK);    最终线程T1 =新的Thread(){
        公共无效的run(){
            尝试{                视频下载(10000);
                完();
                // this.destroy();
            }赶上(例外五){
                // TODO自动生成catch块
                Toast.makeText(getApplicationContext(),e.​​getMessage(),
                        Toast.LENGTH_SHORT).show();
            }
        }
    };
    t1.start();
}保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == RESULT_OK){
        如果(结果code == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
            //成功,创造了TTS实例
            TTS =新的文字转语音(这一点,这一点);
        }其他{
            //丢失的数据,安装
            意图installIntent =新的Intent();
            installIntent
                    .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }}公共无效的OnInit(INT状态){
    如果(状态== TextToSpeech.SUCCESS){
        Toast.makeText(这一点,
                写和听发动机的初始化和放大器;就绪,
                Toast.LENGTH_LONG).show();
    }否则如果(状态== TextToSpeech.ERROR){
        Toast.makeText(这一点,
                在初始化文本到语音引擎时发生错误
                Toast.LENGTH_LONG).show();
    }
}保护无效的onDestroy(){
    super.onDestroy();
    如果(TTS!= NULL){
        tts.shutdown();
    }
}}


解决方案

的OnCreate 您写这code,

  = TTS文字转语音新(这一点,这一点);
    tts.speak(欢迎光临,TextToSpeech.QUEUE_ADD,NULL);
    mImageView =(ImageView的)findViewById(R.id.imageView1);

当时引擎没有安装的过程中它的错误,并退出,从不安装引擎,

所以,首要任务是在的OnCreate 要做的应该是,

 意图checkIntent =新意图();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent,RESULT_OK);

您正在做后长,并试图在线它的安装之前

这样写意图发动机入住在onCreate.Everything第一行是其他美术我明白了。

I am trying make such application which speak automatically when the image loaded it self.I had tried a single application (example) of TTS which is working properly but this code gives me My Code is Text To Speech service isn't started.

public class show_image extends Activity implements TextToSpeech.OnInitListener {

Intent mIntent;
String value;
ImageView mImageView;
private TextToSpeech tts;
int j;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mIntent = getIntent();
    Bundle b = getIntent().getExtras();
    final String name = b.getString("name");
    j = name.toCharArray()[0];

    tts = new TextToSpeech(this, this);
    tts.speak("Welcome", TextToSpeech.QUEUE_ADD, null);
    mImageView = (ImageView) findViewById(R.id.imageView1);

    switch (j) {
    case 65:
        mImageView.setBackgroundResource(R.drawable.a);
        tts.speak(name + " for Apple", TextToSpeech.QUEUE_ADD, null);
        // Toast.makeText(getApplicationContext(),
        // "Saying:" + name + " for Apple", Toast.LENGTH_SHORT)
        // .show();
        tts.isLanguageAvailable(Locale.ENGLISH);
        break;
    case 66:
        mImageView.setBackgroundResource(R.drawable.b);
        tts.speak(name + "for Bluetooth", TextToSpeech.QUEUE_ADD, null);
        break;
    case 67:
        mImageView.setBackgroundResource(R.drawable.c);
        tts.speak(name + "for Chat", TextToSpeech.QUEUE_ADD, null);
        break;
    case 68:
        mImageView.setBackgroundResource(R.drawable.d);
        tts.speak(name + "for Download", TextToSpeech.QUEUE_ADD, null);
        break;
    case 69:
        mImageView.setBackgroundResource(R.drawable.e);
        tts.speak(name + "for E Mail", TextToSpeech.QUEUE_ADD, null);
        break;
    case 70:
        mImageView.setBackgroundResource(R.drawable.f);
        tts.speak(name + "for Facebook", TextToSpeech.QUEUE_ADD, null);
        break;
    case 71:
        mImageView.setBackgroundResource(R.drawable.g);
        tts.speak(name + "for Google", TextToSpeech.QUEUE_ADD, null);
        break;
    case 72:
        mImageView.setBackgroundResource(R.drawable.h);
        tts.speak(name + "for Hewlwtt Packard", TextToSpeech.QUEUE_ADD,
                null);
        break;
    case 73:
        mImageView.setBackgroundResource(R.drawable.i);
        tts.speak(name + "for Iphone", TextToSpeech.QUEUE_ADD, null);
        break;
    case 74:
        mImageView.setBackgroundResource(R.drawable.j);
        tts.speak(name + "for Java", TextToSpeech.QUEUE_ADD, null);
        break;
    case 75:
        mImageView.setBackgroundResource(R.drawable.k);
        tts.speak(name + "for kingston", TextToSpeech.QUEUE_ADD, null);
        break;
    case 76:
        mImageView.setBackgroundResource(R.drawable.l);
        tts.speak(name + "for Laptop", TextToSpeech.QUEUE_ADD, null);
        break;
    case 77:
        mImageView.setBackgroundResource(R.drawable.m);
        tts.speak(name + "for Messenger", TextToSpeech.QUEUE_ADD, null);
        break;
    case 78:
        mImageView.setBackgroundResource(R.drawable.n);
        tts.speak(name + "for Nero", TextToSpeech.QUEUE_ADD, null);
        break;
    case 79:
        mImageView.setBackgroundResource(R.drawable.o);
        tts.speak(name + "for Orkut", TextToSpeech.QUEUE_ADD, null);
        break;
    case 80:
        mImageView.setBackgroundResource(R.drawable.p);
        tts.speak(name + "for Picassa", TextToSpeech.QUEUE_ADD, null);
        break;
    case 81:
        mImageView.setBackgroundResource(R.drawable.q);
        tts.speak(name + "for Quick Heal", TextToSpeech.QUEUE_ADD, null);
        break;
    case 82:
        mImageView.setBackgroundResource(R.drawable.r);
        tts.speak(name + "for Ram", TextToSpeech.QUEUE_ADD, null);
        break;
    case 83:
        mImageView.setBackgroundResource(R.drawable.s);
        tts.speak(name + "for Server", TextToSpeech.QUEUE_ADD, null);
        break;
    case 84:
        mImageView.setBackgroundResource(R.drawable.t);
        tts.speak(name + "for Twitter", TextToSpeech.QUEUE_ADD, null);
        break;
    case 85:
        mImageView.setBackgroundResource(R.drawable.u);
        tts.speak(name + "for Usb", TextToSpeech.QUEUE_ADD, null);
        break;
    case 86:
        mImageView.setBackgroundResource(R.drawable.v);
        tts.speak(name + "for Vista", TextToSpeech.QUEUE_ADD, null);
        break;
    case 87:
        mImageView.setBackgroundResource(R.drawable.w);
        tts.speak(name + "for WiFi", TextToSpeech.QUEUE_ADD, null);
        break;
    case 88:
        mImageView.setBackgroundResource(R.drawable.x);
        tts.speak(name + "for XP", TextToSpeech.QUEUE_ADD, null);
        break;
    case 89:
        mImageView.setBackgroundResource(R.drawable.y);
        tts.speak(name + "for Youtube", TextToSpeech.QUEUE_ADD, null);
        break;
    case 90:
        mImageView.setBackgroundResource(R.drawable.z);
        tts.speak(name + "for Zorpia", TextToSpeech.QUEUE_ADD, null);
        break;
    default:
        break;
    }

    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent, RESULT_OK);

    final Thread t1 = new Thread() {
        public void run() {
            try {

                Thread.sleep(10000);
                finish();
                // this.destroy();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Toast.makeText(getApplicationContext(), e.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }
        }
    };
    t1.start();
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RESULT_OK) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            // success, create the TTS instance
            tts = new TextToSpeech(this, this);
        } else {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent
                    .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }

}

public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        Toast.makeText(this,
                "Write and Listen engine is initialized & Ready",
                Toast.LENGTH_LONG).show();
    } else if (status == TextToSpeech.ERROR) {
        Toast.makeText(this,
                "Error occurred while initializing Text-To-Speech engine",
                Toast.LENGTH_LONG).show();
    }
}

protected void onDestroy() {
    super.onDestroy();
    if (tts != null) {
        tts.shutdown();
    }
}

}
解决方案

In OnCreate You have Written this code,

    tts = new TextToSpeech(this, this); 
    tts.speak("Welcome", TextToSpeech.QUEUE_ADD, null); 
    mImageView = (ImageView) findViewById(R.id.imageView1); 

At that time Engine is not installed so it Gives error and Quit and never goes to install the Engine,

So First task to do in OnCreate should be,

    Intent checkIntent = new Intent(); 
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
    startActivityForResult(checkIntent, RESULT_OK); 

Which you are doing long after and trying to speak before it's installed

So write that intent for Engine Check as first line in onCreate.Everything else is Fine I see.

这篇关于文本到语音服务未在android系统启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 01:04