TextToSpeech を使用しようとしていますが、奇妙な問題がいくつかあります。
私のアプリケーションのアーキテクチャを簡単に説明しましょう。このアプリケーションは、バスの時間に使用されます。ユーザーはバスを通知に追加できます。何か変更があった場合、サーバーはサブスクライバーに通知します。このアプリケーションで TTS を使用したいと考えています。
Android GCM と通知システムを実装しましたが、唯一の問題は TTS です。
GCMIntentService.onMessage
メソッドは、以下の generateNotification メソッドを呼び出します。
public static void generateNotification(Context context, String message) {
int icon = R.drawable.icon_small;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Bundle b = new Bundle();
b.putString(EXTRA_MESSAGE, message);
b.putString(READABLE_MESSAGE, message);
Intent notificationIntent = new Intent(context, BusAlerts.class);
notificationIntent.putExtra("CallType", CallType.NOTIFICATION);
notificationIntent.putExtra("MessageReceived", b);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND;
long[] vibrate = { 1000 };
notification.vibrate = vibrate;
notificationManager.notify(0, notification);
}
このコードは、通知を正常に生成します。これがBusAlerts.codesです(一部トリミングされています;))
public class BusAlerts extends Activity implements OnInitListener {
private static TextToSpeech myTts;
@Override
public void onCreate(Bundle savedInstanceState) {
myTts = new TextToSpeech(this, null);
}
@Override
protected void onStart() {
super.onStart();
//readableMessage extracts from bundles
speak(readableMessage);
}
public void speak(String text)
{
myTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (myTts != null) {
myTts.shutdown();
myTts.stop();
myTts = null;
}
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = myTts.setLanguage(Locale.UK);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
}
else{
if (myTts.isLanguageAvailable(Locale.UK) == TextToSpeech.LANG_AVAILABLE || myTts.isLanguageAvailable(Locale.UK) == TextToSpeech.LANG_COUNTRY_AVAILABLE)
myTts.setLanguage(Locale.UK);
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
ユーザーが通知からこのクラスを開くと、すべてが完全に機能しますが、音が出ません。デバッグすると、次のような状況が表示されます。
ただし、ユーザーがホームボタンを押してアプリケーションを再度開くと (同じアクティビティが再開されます)、ユーザーはサウンドを聞くことができます。その様子はこんな感じ。
私が気付いたように、mCachedParamters と mITts は異なります。しかし、理由はわかりません。私は一週間それで立ち往生しており、解決策を見つけることができませんでした.