2011-03-07 59 views
5

我試圖讓Android的TTS在服務中運行,但我不知道爲什麼它不工作,它編譯,不會崩潰,但它不起作用。在服務中運行Android TTS

Toast通知雖然可以工作。

package alarm.test; 

import android.app.Service; 
import com.google.tts.TextToSpeechBeta; 
import android.content.Intent; 
import android.os.IBinder; 
import android.widget.Toast; 

public class MyAlarmService extends Service { 

    private TextToSpeechBeta myTts; 
    private TextToSpeechBeta.OnInitListener ttsInitListener = new TextToSpeechBeta.OnInitListener() { 
     public void onInit(int arg0, int arg1) { 
      myTts.speak("", 0, null); 
     } 
    }; 

@Override 
public void onCreate() { 
// TODO Auto-generated method stub 
    myTts = new TextToSpeechBeta(this, 
      ttsInitListener); 

Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show(); 
} 

@Override 
public IBinder onBind(Intent intent) { 
// TODO Auto-generated method stub 
    myTts.speak("something is working", TextToSpeechBeta.QUEUE_FLUSH, null); 
Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show(); 
return null; 
} 

@Override 
public void onDestroy() { 
// TODO Auto-generated method stub 
super.onDestroy(); 
Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onStart(Intent intent, int startId) { 
// TODO Auto-generated method stub 
super.onStart(intent, startId); 
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show(); 
} 

@Override 
public boolean onUnbind(Intent intent) { 
// TODO Auto-generated method stub 
Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show(); 
return super.onUnbind(intent); 
} 

} 
+0

請更詳細地描述你的意思是「不工作」。你在LogCat上看到什麼? – 2011-03-07 12:16:39

+0

DEBUG/TextToSpeechBeta(512):TextToSpeechBeta不instled - dfauing基本平臺文字轉語音的說話 VERBOSE/TtsSice(418):TTS服務接收 INFO/TtsService(418):回採 INFO/TtsService(418):停止 VERBOSE/TtsService(418):TTS處理: VERBOSE/TtsService(418):TtsService.setLanguage(eng,USA) INFO/SVOX Pico引擎(418):語言已加載(en-US == en-US) DEBUG/dalvikvm(68):GC_CONCURRENT釋放719K,48%空閒4093K/7751K,外部2041K/2378K,暫停21ms + 27ms DEBUG/SntpClient(68):請求時間失敗:java.net.SocketException:地址族不受支持協議 – 2011-03-07 19:35:27

回答

14

你可以象下面這樣:它是爲我工作。 您必須創建活動啓動此服務,像這樣:this.startService(意向)

public class TTSService extends Service implements TextToSpeech.OnInitListener{ 

private String str; 
private TextToSpeech mTts; 
private static final String TAG="TTSService"; 

@Override 

public IBinder onBind(Intent arg0) { 

    return null; 
} 


@Override 
public void onCreate() { 

     mTts = new TextToSpeech(this, 
       this // OnInitListener 
       ); 
     mTts.setSpeechRate(0.5f); 
     Log.v(TAG, "oncreate_service"); 
    str ="turn left please "; 
    super.onCreate(); 
} 


@Override 
public void onDestroy() { 
    // TODO Auto-generated method stub 
    if (mTts != null) { 
      mTts.stop(); 
      mTts.shutdown(); 
     } 
     super.onDestroy(); 
} 

@Override 
public void onStart(Intent intent, int startId) { 


    sayHello(str); 

    Log.v(TAG, "onstart_service"); 
    super.onStart(intent, startId); 
} 

@Override 
public void onInit(int status) { 
    Log.v(TAG, "oninit"); 
    if (status == TextToSpeech.SUCCESS) { 
      int result = mTts.setLanguage(Locale.US); 
      if (result == TextToSpeech.LANG_MISSING_DATA || 
       result == TextToSpeech.LANG_NOT_SUPPORTED) { 
       Log.v(TAG, "Language is not available."); 
      } else { 

       sayHello(str); 

      } 
     } else { 
      Log.v(TAG, "Could not initialize TextToSpeech."); 
     } 
} 
private void sayHello(String str) { 
     mTts.speak(str, 
       TextToSpeech.QUEUE_FLUSH, 
       null); 
} 
} 
+0

幫助了很多,謝謝。 – 2014-09-24 09:06:43