我提到了两个链接:
从这两个链接中,我创建了一个AppWidgetProvider类和一个Service类,如下所示:
public class HomeWidget extends AppWidgetProvider{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
ComponentName thisWidget = new ComponentName(context,
HomeWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
Intent homeIntent = new Intent(context.getApplicationContext(), VoiceService.class);
homeIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
context.startService(homeIntent);
}
}
服务:
public class VoiceService extends Service implements OnInitListener{
public static TextToSpeech mtts;
private static final String LOG = "VoiceService";
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d(LOG,"Service created successfully!");
mtts = new TextToSpeech(getApplicationContext(),this);
mtts.setLanguage(Locale.ENGLISH);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(mtts!=null)
{
mtts.stop();
Log.d(LOG,"The service has been destroyed!");
}
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
.getApplicationContext());
int[] allWidgetIds = intent
.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName thisWidget = new ComponentName(getApplicationContext(),
HomeWidget.class);
int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length));
Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length));
for(int widgetId : allWidgetIds)
{
Log.w(LOG,"inside for loop");
RemoteViews remoteViews = new RemoteViews(this
.getApplicationContext().getPackageName(),
R.layout.home_widget);
mtts = new TextToSpeech(getApplicationContext(),this);
mtts.setLanguage(Locale.ENGLISH);
mtts.speak("This is the text to be spoken", TextToSpeech.QUEUE_FLUSH, null);
Intent clickIntent = new Intent(this.getApplicationContext(),HomeWidget.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.home, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
super.onStart(intent, startId);
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
}
我得到了小部件,当我点击小部件时,logcat中出现了这6行:
08-03 02:12:55.354: D/VoiceService(3666): Service created successfully!
08-03 02:12:55.358: W/VoiceService(3666): From Intent1
08-03 02:12:55.358: W/VoiceService(3666): Direct1
08-03 02:12:55.358: W/VoiceService(3666): inside for loop
08-03 02:12:55.358: I/TTS received:(3666): This is the text to be spoken
08-03 02:12:55.370: D/VoiceService(3666): The service has been destroyed!
但我没有听到任何声音。我已经达到最大音量,确保它使用语音合成引擎(Pico TTS)并安装英语(美国)和英语(英国)。我还安装了语音合成所需的语音数据。
我还缺少什么或做错了,以至于没有发言权?
答案 0 :(得分:0)
在将任何文本发送到tts之前,您必须等待初始回调,因为构造函数是一个异步操作,在您准备就绪之前返回。将调用移至onInit调用或从onInit调用的内容。