0

I have this code that initializes text to speech. However, text to speech takes around ten seconds to load so I would like it to be done in async. How can I do this without changing the functionality of my code?

import java.util.Locale;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.content.res.Resources;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Button;
import java.lang.String;
import java.util.Random;

public class MainActivity extends Activity implements TextToSpeech.OnInitListener{

    TextToSpeech tts;
    private static final Random r_generator = new Random();
    String textViewString;
    int MY_DATA_CHECK_CODE = 1234;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        Resources res = getResources();
        TextView tv = (TextView) findViewById(R.id.animal_text);
        String loadingString = res.getString(R.string.Loading);
        tv.setText(loadingString);

        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

    }

    protected void onActivityResult(
            int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // success, create the TTS instance
                tts = new TextToSpeech(this, this);
            } else {
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }
    }

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

        Resources res = getResources();
        TextView tv = (TextView) findViewById(R.id.animal_text);
        String[] myString = res.getStringArray(R.array.englishAnimalArray);
        String q = myString[r_generator.nextInt(myString.length)];
        tv.setText(q);

        textViewString = tv.getText().toString();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            tts.speak(textViewString, TextToSpeech.QUEUE_FLUSH, null);
        } else {
            tts.speak(textViewString, TextToSpeech.QUEUE_FLUSH, null, null);
        }

    }
lionnn
  • 1
  • 2

1 Answers1

1

I'd suggest you can take this by using AsyncTask.

An AsyncTask works as follows:

private ProgressDialog pDialog; // Progress Dialog to load during the AsyncTask

private class InitializeSpeechEngine extends AsyncTask<Void, Void, Void>{
   @Override
   protected void onPreExecute(Void aVoid){
      // Initialization code goes inside onPreExecute
      pDialog = new ProgressDialog(MainActivity.this);
      pDialog.setTitle("Loading Speech to Text Engine");
      pDialog.setMessage("Please Wait...");
      pDialog.setCancellable(false);
      pDialog.show();
   }

   @Override
   protected void doInBackground(Void... params){
      // Perform the speech to text initialization here
   }

   @Override
   protected void onPostExecute(Void aVoid){
      // Display Toast that the engine is ready for use.
      pDialog.dismiss();
      Toast.makeText(MainActivity.this, "Speech to text engine initialization complete", Toast.LENGTH_SHORT).show();
   }

   @Override 
   protected void onProgressUpdate(Void... params){

   }

You can start executing the AsyncTask by making the following call.

new InitialiazeSpeechEngine().execute();
Aditya
  • 1,172
  • 11
  • 32