-1
我想爲android應用程序編寫一個java代碼,它將我的文本轉換爲語音,但我在getText的makeText方法中出錯。我是Android新手,請幫助我。我正的錯誤是Android文本到語音轉換
在類型吐司的方法makeText(上下文,CharSequence中,INT)不適用的參數(文字轉語音,字符串, 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");
}
}
}
您正在傳遞'TexttoSpeech'的實例,而不是來自Toast的上下文。用正確的上下文替換makeText中的TexttoSpeech.this。 – csmckelvey