文本到语音类不起作用(logcat中没有错误)

时间:2014-03-28 16:04:32

标签: android android-intent

我正在制作一个电话阅读器应用程序。在这个应用程序中,我使用BroadcastReceiver来了解它们是否是来电。如果他们是来电,则调用Myservice.class,其中我使用了Text to speech类,它将文本转换为通过意图获得的语音。 我已经使用日志检查过程,一切都很好,他们没有错误。但我不知道我的申请是如何不说名字的。广播听众工作正常。任何人都可以帮忙。 谢谢。如果已经粘贴了服务类的代码。

MyService.class

package com.example.sms_reader;

import java.util.Locale;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.speech.tts.TextToSpeech;
import android.util.Log;

public class Myservice extends Service implements TextToSpeech.OnInitListener{
     TextToSpeech tts;
     String   content2;
     int pitch =3 ;
     int speech = 3;
     public void onCreate() {       
        super.onCreate();


     }               
     public int onStartCommand(Intent intent, int flags, int startId) {
            //TODO do something useful        
         Context context = getApplicationContext();
         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    //   pitch = prefs.getInt("SET_PITCH", 3);
        // speech = prefs.getInt("SET_SPEECH", 3);
         tts = new TextToSpeech(this, this);    
         Log.d("tag4", "INTENT RECEIVED");
             content2 = intent.getStringExtra("content");       
             Log.d("tag6", content2);


         return Service.START_NOT_STICKY;

     }


private void speakOut() {

    Log.d("tag","Speak function gets a call "); 
    tts.speak(content2, TextToSpeech.QUEUE_FLUSH, null);
   // stopSelf();
}   

@Override
public void onDestroy() {
    // Don't forget to shutdown!
    if (tts != null) {
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
}
@Override
public void onInit(int status) {
    // TODO Auto-generated method stub

    Log.d("tag","Init is called");
    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;
    }

}

0 个答案:

没有答案