Search code examples
androidlocaletext-to-speechpersianespeak

Espeak reads separate letters instead of words


At the first time, I tried to use the Google's default TTS engine in the following code, but I found that, the Persian language is not supported!

So, I downloaded and installed espeak RedZoc TTS engine on my phone and changed the default TTS language to Persian. And when I checked it in my phone settings or inside RedZoc app, it works well.

But when I run my code inside my phone, it reads the letters separately instead of reading the complete word! (for example it should say SALAM but it says Arabic Sin Lam Alef Mim )

MainActivity:

package com.m.ttsapp;

import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.Locale;

public class MainActivity extends AppCompatActivity
{
    String text;
    EditText et;
    TextToSpeech tts;
    Button btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et=findViewById(R.id.editText1);
        btn = findViewById(R.id.button1);

        tts=new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {


            @Override
            public void onInit(int status)
            {
                if(status == TextToSpeech.SUCCESS)
                {
                    int result=tts.setLanguage(Locale.);
                    if(result==TextToSpeech.LANG_MISSING_DATA || result==TextToSpeech.LANG_NOT_SUPPORTED)
                    {
                        Log.e("error", "This Language is not supported");
                    }
                    else{
                        ConvertTextToSpeech();
                    }
                }
                else
                    Log.e("error", "Initilization Failed!");
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConvertTextToSpeech();

            }
        });

    }

    @Override
    protected void onPause()
    {

        if(tts != null)
        {

            tts.stop();
            tts.shutdown();
        }
        super.onPause();
    }


    private void ConvertTextToSpeech()
    {
        text = et.getText().toString();
        if(text == null || "".equals(text))
        {
            text = "Content not available";
            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
        }else
            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }


}

I know maybe I must change this line of the code but don't know changing it to what? int result=tts.setLanguage(Locale.);

Or maybe I must forget all of this code and write another one? But how?


Solution

  • There is no 'inbuilt' Locale.PERSIAN and therefore you need to create the Locale.

    final Locale persianLocale = new Locale("fa","IR");
    

    And then set it:

    tts.setLanguage(persianLocale);