2
我有一個服務,我試圖啓動一個TextToSpeech引擎,但它似乎不工作,所以是否有可能從服務啓動tts?從服務啓動文本到語音引擎?
這裏就是我試過:
package com.example.TextSpeaker;
import java.util.Locale;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.widget.Toast;
public class SpeakerService extends Service implements OnInitListener{
public static TextToSpeech mtts;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
Log.d("SpeakerService","Service created successfully!");
mtts = new TextToSpeech(this,this);
mtts.setLanguage(Locale.ENGLISH);
}
@Override
public void onStart(Intent intent,int startid)
{
Log.d("SpeakerService","Service started successfully!");
Log.d("SpeakerService","Service started successfully!");
Log.d("SpeakerService","tspker.mtts = " + TextSpeaker.mtts.toString());
mtts = new TextToSpeech(this,this);
mtts.setLanguage(Locale.ENGLISH);
mtts.speak(Receiver.str, TextToSpeech.QUEUE_FLUSH,null);
}
@Override
public void onDestroy(){
if(mtts!=null)
{
mtts.stop();
Toast.makeText(getApplicationContext(),"The service has been destroyed!", T oast.LENGTH_SHORT).show();
}
}
@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
}
}
「但一定要讓服務運行,直到演講做。」 你的意思它會運行,如果我不通過設置手動殺死服務? – pranay 2010-09-10 13:53:59
from:http://android-developers.blogspot.com/2009/09/introduction-to-text-to-speech-in.html當不使用時... 文字轉語音功能依賴於在使用該功能的所有應用程序之間共享專用服務。當你完成使用TTS時,成爲一個好公民並通過在你的Activity onDestroy()方法中調用mTts.shutdown()來告訴它「你不再需要它的服務了」。 – arnold 2010-10-14 21:02:10