Giter Site home page Giter Site logo

Comments (1)

enock-meston avatar enock-meston commented on August 9, 2024

package com.example.texttospeechexample;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private static final int TTS_ENGINE_REQUEST =101;
private TextToSpeech textToSpeech;

private EditText TextForSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextForSpeech = (EditText)findViewById(R.id.speech_text);
}

public void performSpeech(View view) {
    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent,TTS_ENGINE_REQUEST);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == TTS_ENGINE_REQUEST && resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
        textToSpeech = new TextToSpeech(this, this);
    } else {
        Intent installIntent = new Intent();
        installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
        startActivity(installIntent);
    }
}

@Override
public void onInit(int status) {
    if (status==TextToSpeech.SUCCESS){
        int languageStatus = textToSpeech.setLanguage(Locale.US);
        if (languageStatus==TextToSpeech.LANG_MISSING_DATA || languageStatus==TextToSpeech.LANG_NOT_SUPPORTED){
            Toast.makeText(this, "Language is not Support..", Toast.LENGTH_SHORT).show();
        }else {
            String data = TextForSpeech.getText().toString();
            int speechStatus = textToSpeech.speak(data,TextToSpeech.QUEUE_FLUSH,null);
            if (speechStatus==TextToSpeech.ERROR){
                Toast.makeText(this, "Error while Speech..", Toast.LENGTH_SHORT).show();
            }
        }
    }else{
        Toast.makeText(this, "Text to speech engine failed..", Toast.LENGTH_SHORT).show();
    }
}

}

from softenido.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.