在Android的全球TTS [英] Global TTS in Android

查看:168
本文介绍了在Android的全球TTS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发民为盲人用户的应用程序,这样我经常用到的文本到语音的practicaly的只有一个方法如何在响应用户的操作。 我决定,只要应用一个全局TTS实例中运行。 我已经实现了它这样

Hi I am devloping an application for blind users so that I use very often text to speech as practicaly the only one method how to respond on user actions. I decided to make one global TTS instance running as long as the app. I have implemented it this way

package com.simekadam.blindguardian;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;

public class SpeechHelper implements OnInitListener {

private static TextToSpeech mTts;
private String text; 
private static final SpeechHelper helper = new SpeechHelper();

public static SpeechHelper getInstance(){

    return helper;
}


public void say(String text, Context context){

    if(mTts == null){
        this.text = text;
        mTts = new TextToSpeech(context, (OnInitListener) helper);

    }
    else{
        mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}


@Override
public void onInit(int status) {
    // TODO Auto-generated method stub
    if (status == TextToSpeech.SUCCESS) {
        mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}

public void stopTTS(){
    if(mTts != null){
        mTts.shutdown();
        mTts.stop();
        mTts = null;
    }
}

}

起初 - 它的工作,但 ..我要检查语音数据这样的可用性

At first - its working BUT ..I wanted to check the availability of speech data like that

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
    text = getIntent().getExtras();
}


protected 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, (OnInitListener) this);
            mTts.setLanguage(new Locale("cze", "CZE"));
        } else {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(
                TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }       
}

这是Android开发者门户网站其code,但我不能启动活动的结果,从类这不是android.Activity的孩子.. 请怎么检查它不使用活动,并为它调用TTS的这种做法正确吗? (我已经实现这一切与活动前,但有一对夫妇的内存泄漏,由于不正确地关闭TTS - 当我正确关闭它,它必须再次被创建在每次调用 - 只是太慢了。)

Its code from Android developer portal, but I cant start Activity for result from class which is not child of android.Activity.. Please how to check it without using activities, and is it this approach of invoke TTS correct? (I have implemented it all with Activities before, but there was a couple of memory leaks, due to incorrectly closed TTS - and when I closed it properly, it must been created again on every call - just too slow..)

推荐答案

onActivityResult初始化的全局实例(),之后您知道TTS数据可用。您的应用程序需要一个活动,所以它从入门的活动,所有后续的人将能够使用您的全局实例一旦被初始化。想想也是,当你将如何将它关闭。

Initialize your global instance from onActivityResult(), after you know that TTS data is available. Your app needs an activity, so do it from the entry activity, all subsequent ones will be able to use your global instance once it is initialized. Also think about when and how you will shut it down.

这篇关于在Android的全球TTS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆