📜 ⬆️ ⬇️

Speech synthesizer Now in Russian

I was always upset that Android did not have a speech synthesizer in Russian. Initially, the choice of languages ​​was limited to English, Spanish, French, German and Italian. There were separate commercial engines, as well as manufacturers could add to their devices any engine with the desired language, apparently having agreed with the developer. But I wanted support from the box from the “corporation of good” itself.

Yesterday, having turned on WiFi on my phone in the subway, I saw that several updates had arrived on the device, including the Google Voice Synthesizer with the support of the Russian language. At first, I did not pay attention to this circumstance, but today I suddenly thought, but can I not now use the Russian-language engine in my application?
Sketched a simple example with a button to hear a phrase from the classics: “And Vaska listens and eats.”

//    ,    , //   ,   ,   . 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(); } } 


Having launched the application, I heard my native speech, uttered by a female voice.
First, I used the default locale Locale.getDefault (). For most Russian users, this will work, but some users leave a different locale on the phone, so I decided to explicitly specify Locale (“ru”). Probably so more correct.
If the Google Speech Synthesizer hasn't arrived yet, you can download it from Google Play at https://play.google.com/store/apps/details?id=com.google.android.tts .

')

Source: https://habr.com/ru/post/224685/


All Articles