2016-08-01 14 views
0

私はこのコードを使用して、テキストを音声に初期化します。しかし、音声読み上げには約10秒かかりますので、非同期で行うことをお勧めします。コードの機能を変更せずにこれを行うにはどうすればよいですか?非同期でテキストを音声に読み込む方法

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

    } 
+0

なぜGoogleのテキストテキストを使用してみてください。私はそれを使用しており、初期化も時間もかかりません。 – aditya

+0

この記事を見るhttp://stackoverflow.com/q/36013611/1256219 – brandall

答えて

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(); 
関連する問題