2014-03-27 55 views
0

嗨,我是一個在android開發新手,我正在創建一個短信和通話閱讀器應用程序,其中我已經使用了一個服務,我已經使用文本到語音類。並且我收到錯誤發言失敗:未綁定到TTS引擎。誰能幫忙? 由於文本到語音類顯示錯誤(安卓)

Texttospeech.java 

package com.example.sms_reader; 

import java.util.Locale; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.speech.tts.TextToSpeech; 
import android.util.Log; 

public class Myservice extends Service{ 
    TextToSpeech tts; 
    String content2; 
    public void onCreate() {  
     super.onCreate(); 
     tts = new TextToSpeech(this, null); 

    }    
    public int onStartCommand(Intent intent, int flags, int startId) { 
      //TODO do something useful   
      Log.d("tag4", "INTENT RECEIVED"); 
      content2 = intent.getStringExtra("content");  
      Log.d("tag6", content2); 
      speakOut(); 

     return Service.START_NOT_STICKY; 

    } 


private void speakOut() { 


    tts.speak(content2, TextToSpeech.QUEUE_FLUSH, null); 
} 

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

public void onInit(int status) { 
    // TODO Auto-generated method stub 

    if (status == TextToSpeech.SUCCESS) { 

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

     // tts.setPitch(5); // set pitch level 

     // tts.setSpeechRate(2); // set speech speed rate 

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

      speakOut(); 
     } 

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

} 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

} 

回答

2

更換TTS =新文字轉語音(此,NULL);由tts = new TextToSpeech(this,this);

+0

@ Ajit-no它顯示錯誤,如果我這樣做。 –

+0

替換公共類Myservice extends Service {with public class Myservice extends Service implements TextToSpeech.OnInitListener { 並將重寫註釋(@Override)添加到init()方法 – Ajit

+0

@ Ajit-現在應用程序正在工作,但它仍顯示相同的錯誤。它只有一個名字,當一個戒指完成並且另一個戒指開始時它不會說出名字 –