昨日、地下鉄で携帯電話のWiFiをオンにすると、ロシア語をサポートするGoogle Speech Synthesizerなど、いくつかの更新がデバイスに届くことがわかりました。 最初はこの状況に注意を払わなかったので、今日は突然考えましたが、アプリケーションでロシア語のエンジンを使用できますか?
古典からのフレーズを聞くためのボタンを備えた簡単な例をスケッチしました:「そしてVaskaは聞いて食べる」。
// , , // , , . package ru.alexanderklimov.tts; import java.util.Locale; import android.app.Activity; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity implements TextToSpeech.OnInitListener { private Button mButton; private TextToSpeech mTTS; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTTS = new TextToSpeech(this, this); mButton = (Button) findViewById(R.id.button1); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String text = " "; mTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null); } }); } @Override public void onInit(int status) { // TODO Auto-generated method stub if (status == TextToSpeech.SUCCESS) { Locale locale = new Locale("ru"); int result = mTTS.setLanguage(locale); //int result = mTTS.setLanguage(Locale.getDefault()); if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) { Log.e("TTS", ", "); } else { mButton.setEnabled(true); } } else { Log.e("TTS", "!"); } } @Override public void onDestroy() { // Don't forget to shutdown mTTS! if (mTTS != null) { mTTS.stop(); mTTS.shutdown(); } super.onDestroy(); } }
アプリケーションを起動すると、女性の声によるネイティブのスピーチが聞こえました。
最初は、デフォルトのロケールLocale.getDefault()を使用しました。 ほとんどのロシアのユーザーにとってこれは機能しますが、一部のユーザーは電話で別のロケールを残すため、Locale( "ru")を明示的に指定することにしました。 おそらくより正しい。
Google Speech Synthesizerのアップデートがまだ届いていない場合は、 https://play.google.com/store/apps/details?id = com.google.android.ttsからGoogle Playからダウンロードできます 。