Android文字转语音转换

时间:2014-03-01 07:01:54

标签: android

我正在尝试为Android应用程序编写一个java代码,它将我的文本转换为语音,但我在Toast的makeText方法中出错。我是android新手所以请帮助我。我得到的错误是

Toast类型中的方法makeText(Context,CharSequence,int)不适用于参数(TexttoSpeech,String,  INT)

这是我的代码

package com.example.messagereader;

import java.util.Locale;

import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.widget.Toast;

public class TexttoSpeech {


    private TextToSpeech tts;
    private void speakOut()
{

    String num = null;
    String mes = null;
    String text ="Message From "+num+"Message Body :"+mes;
    if (text.length() == 0)
    {
        tts.speak("You haven't typed text", TextToSpeech.QUEUE_FLUSH,
                null);
    }
    else
    {
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}
public void onInit(int status)
{
    if (status == TextToSpeech.SUCCESS)
    {
        int result = tts.setLanguage(Locale.US);
        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED)
        {
            Toast.makeText(TexttoSpeech.this, "Language not Supported",
                    Toast.LENGTH_LONG).show();

            Log.e("TTS", "Language is not supported");
        }
    }
    else
    {
        Toast.makeText(this, "TTS Initilization Failed", Toast.LENGTH_LONG).show();
        Log.e("TTS", "Initilization Failed");
    }
}


}

1 个答案:

答案 0 :(得分:0)

您需要从您的活动传递给您的TexttoSpeech上下文。在您的代码中thisTexttoSpeech的引用,而Toast.makeText需要引用Context作为其第一个参数。 Activity类派生自Context,因此您可以从Activity中将this传递给您的班级。

[编辑]

为了显示Toast-s,您还可以应用Context,这意味着您还可以显示来自服务的Toast。要获取应用程序上下文,请在上下文引用上调用getApplicationContext。