我有以下pgm迴應與傳入味精吐司,也說出收到的味精,但似乎沒有語音合成的背景,但我仍然可以看到吐司雖然,所以我應該從onReceive方法啓動一個服務(不知道這是否可能),然後在startService方法中,編寫speak方法?與BroadcastReceiver混淆
這裏是我的鉑族金屬之一:
接收機
package com.example.TextSpeaker;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class Receiver extends BroadcastReceiver{
public static String str;
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Log.d("Receiver","Message received successfully");
SmsMessage[] msgs = null;
if(bundle!=null)
{
// retrive the sms received
Object[] pdus = (Object[])bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0;i<msgs.length;i++)
{
str="";
msgs[i]=SmsMessage.createFromPdu((byte[]) pdus[i]);
str+="Message From "+msgs[i].getOriginatingAddress()+". ";
str+="The message is "+msgs[i].getMessageBody().toString();
//TextSpeaker.mtts.speak(Receiver.str, TextToSpeech.QUEUE_FLUSH,null);
//str="";
}
Toast.makeText(context,str,Toast.LENGTH_LONG).show();
TextSpeaker.mtts.speak(Receiver.str, TextToSpeech.QUEUE_FLUSH,null);
}
}
}
感謝它的工作!但是,雖然我在日誌中看到服務已經啓動,但我聽不到任何聲音,但是tts引擎是否在後臺運行? – pranay 2010-06-24 10:26:52
AKAIK是的,tts引擎一直在後臺運行...如果它沒有在你的日誌中顯示任何錯誤,我想這意味着它的工作正常......順便說一句,你是否試圖在仿真器或設備上使用它? ? – JaVadid 2010-06-25 05:15:13