【问题标题】:How to load text to speech in async如何异步加载文本到语音
【发布时间】:2016-08-01 21:40:41
【问题描述】:

我有这段代码将文本初始化为语音。但是,文本到语音的加载大约需要十秒钟,所以我希望它异步完成。如何在不更改代码功能的情况下做到这一点?

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);
        }

    }

【问题讨论】:

标签: java android android-asynctask text-to-speech


【解决方案1】:

我建议您可以使用 AsyncTask 来完成此任务。

AsyncTask 的工作原理如下:

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){

   }

您可以通过以下调用开始执行 AsyncTask。

new InitialiazeSpeechEngine().execute();

【讨论】:

    猜你喜欢
    • 2012-06-18
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多