Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text-To-Speech in Android That Works Offline [closed]

My friend and I are developing an app that uses deep learning and neural nets to help visually impaired people. We're looking for a way to bring the information that the neural nets get via the smartphone's camera back to the user via voice, thus we need to do TextToSpeech.

However, it's a HUGE, HUGE deal for the users to have the app work offline and as all the other parts of the app are capable of running with no Internet connection (neural nets etc.) we're looking for a way to do TextToSpeech offline. The app's also in Russian, so something that can support multiple languages would be great.

We'd be SUPER grateful for any hints on where to start with offline TextToSpeech on Android in Android Studio, thanks!

like image 486
Ivan Goncharov Avatar asked Oct 25 '25 09:10

Ivan Goncharov


1 Answers

Try this out. Make sure to add a text input box and button in your xml layout

import java.util.Locale;
import android.speech.tts.TextToSpeech;

public class TextToSpeech {

    private EditText write;
    private TextToSpeech t1;
    private Button speakbtn;

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

        write = (EditText) findViewById(R.id.editText);
        speakbtn = (Button) findViewById(R.id.board);

        t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    t1.setLanguage(Locale.ENGLISH);
                }
            }
        });
        speakbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String toSpeak = write.getText().toString();
                Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
                t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
            }
        });
    }
    
    @Override
    public void onDestroy() {
        // Dont forget to shut down text to speech
        if (t1 != null) {
            t1.stop();
            t1.shutdown();
        }
        super.onDestroy();
    }
}
like image 89
Benjamin Hue Avatar answered Oct 28 '25 01:10

Benjamin Hue