2017-05-06 39 views
0

我在我的Android應用中實現了一個非常基本的TTS,當應用在前臺運行時似乎工作正常,但在後臺運行時不起作用。它會等到應用程序返回到前臺並播放隊列中的內容。我的GPS追蹤應用程序如何在顯示屏關閉時提供跟蹤反饋,從而實現這一目標? 我的主要類實現: TextToSpeech.OnInitListener文本到語音 - 不在後臺工作

我排隊TTS有:

ttsProgressStr = "Time " + 
       timeStringForVoice(trackingTimeMillis); 
ttsEngine.speak(ttsProgressStr, TextToSpeech.QUEUE_FLUSH, null, null); 
+0

https://開頭code.tutsplus.com/tutorials/android-sdk-using-the-text-to-speech-engine--mobile-8540 –

回答

0

請參閱本

import android.app.Activity; 
import android.os.Bundle; 
import android.speech.tts.TextToSpeech; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import java.util.Locale; 

public class MainActivity extends Activity implements 
    TextToSpeech.OnInitListener { 
/** Called when the activity is first created. */ 

private TextToSpeech tts; 
private Button buttonSpeak; 
private EditText editText; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    tts = new TextToSpeech(this, this); 
    buttonSpeak = (Button) findViewById(R.id.button1); 
    editText = (EditText) findViewById(R.id.editText1); 

    buttonSpeak.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      speakOut(); 
     } 

    }); 
} 

@Override 
public void onDestroy() { 
// Don't forget to shutdown tts! 
    if (tts != null) { 
     tts.stop(); 
     tts.shutdown(); 
    } 
    super.onDestroy(); 
} 

@Override 
public void onInit(int status) { 

    if (status == TextToSpeech.SUCCESS) { 

     int result = tts.setLanguage(Locale.US); 

     if (result == TextToSpeech.LANG_MISSING_DATA 
       || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
      Log.e("TTS", "This Language is not supported"); 
     } else { 
      buttonSpeak.setEnabled(true); 
      speakOut(); 
     } 

    } else { 
     Log.e("TTS", "Initilization Failed!"); 
    } 

} 

private void speakOut() { 
    String text = editText.getText().toString(); 
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 
} 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="77dp" 
    android:layout_marginTop="42dp" 
    android:ems="10" > 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/editText1" 
    android:layout_below="@+id/editText1" 
    android:layout_marginLeft="59dp" 
    android:layout_marginTop="39dp" 
    android:text="Speak" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/editText1" 
    android:layout_alignBottom="@+id/editText1" 
    android:layout_alignParentLeft="true" 
    android:text="Enter Text:" /> 

</RelativeLayout> 
+0

謝謝你的「清潔」代碼Jinal。我修改了我的代碼以適應,但「.speak」方法在應用程序在後臺運行時仍然不起作用。它被排隊,直到應用程序再次處於前臺。我需要它在屏幕關閉時說話... –