2010-06-23 4 views
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 

} 

} 

답변

4

새로운 TextToSpeech (getApplicationContext(),이); 나와 함께

작품 ... 그러나 연설이 완료 될 때까지 서비스 실행을하게해야합니다 ..

+0

"그러나 연설이 완료 될 때까지 서비스 실행을하게해야합니다 .." 는 뜻 내가 설정을 통해 수동으로 서비스를 죽이지 않으면 실행될 것인가? – pranay

+0

출처 : http://android-developers.blogspot.com/2009/09/introduction-to-text-to-speech-in.html 사용하지 않을 때 ... 텍스트 음성 변환 기능은 해당 기능을 사용하는 모든 응용 프로그램에서 공유되는 전용 서비스 TTS를 사용하여 작업을 마쳤 으면 훌륭한 시민이되고 예를 들어 Activity onDestroy() 메서드에서 mTts.shutdown()을 호출하여 "더 이상 서비스가 필요하지 않게됩니다."라고 알립니다. – arnold

관련 문제