2016-08-01 13 views
0

Ich habe diesen Code, der Text in Sprache initialisiert. Text-zu-Sprache dauert jedoch etwa zehn Sekunden, um geladen zu werden, so dass ich es gerne asynchron machen würde. Wie kann ich das tun, ohne die Funktionalität meines Codes zu ändern?So laden Sie Text in Sprache in async

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

Warum nicht versuchen, die Text-to-Speech-Funktion von Google zu verwenden. Ich habe es benutzt und es braucht keine Initialisierung/keine Zeit. – aditya

+0

Sehen Sie diesen Beitrag http://stackoverflow.com/q/36013611/1256219 – brandall

Antwort

1

Ich würde vorschlagen, dass Sie dies mit AsyncTask nehmen können.

Ein AsyncTask funktioniert wie folgt:

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

    } 

Sie beginnen können die AsyncTask Ausführung durch den folgenden Aufruf zu machen.

new InitialiazeSpeechEngine().execute();