This question already has answers here:
Using text to speech APIs in android application

(3 个回答)


5年前关闭。




我正在开发文本到语音应用程序,我想将土耳其语设置为:
tts.setLanguage(Locale.TR);

但是这在android中不可用,以这种方式添加是错误的还是有不同的方式将土耳其语添加到文本到语音中。

任何帮助和建议将不胜感激

文本到语音代码:
 public class AndroidTextToSpeechActivity extends Activity implements
    TextToSpeech.OnInitListener {
/** Called when the activity is first created. */

private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;

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

    tts = new TextToSpeech(this, this);

    btnSpeak = (Button) findViewById(R.id.btnSpeak);

    txtText = (EditText) findViewById(R.id.txtText);

    // button on click event
    btnSpeak.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            speakOut();}
                       });}
@Override
public void onDestroy() {
    // Don't forget to shutdown!
    if (tts != null) {
        tts.stop();
        tts.shutdown();}

    super.onDestroy();}

@Override
public void onInit(int status) {
    // TODO Auto-generated method stub

    if (status == TextToSpeech.SUCCESS) {

        int result = tts.setLanguage(Locale.US);

        // tts.setPitch(5); // set pitch level

        // tts.setSpeechRate(2); // set speech speed rate

        if (result == TextToSpeech.LANG_MISSING_DATA
            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "Language is not supported");
        } else {
            btnSpeak.setEnabled(true);
            speakOut();}

       } else {
            Log.e("TTS", "Initilization Failed");}}

 private void speakOut() {

    String text = txtText.getText().toString();

    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);}}

最佳答案

如果您的设备是土耳其语,请使用:

tts.setLanguage(Locale.getDefault());

代替
int result = tts.setLanguage(Locale.US);

那么 EXTRA_LANGUAGE_MODEL 值必须是:“tr-TR”。

我尝试并成功了。(但我也从三星市场下载了土耳其语 lang.pack)

关于android - 为文本到语音设置土耳其语,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16078799/

10-08 23:59