0

我正在尝试使用 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 不同;但我不知道为什么。我已经卡了一个星期,找不到解决办法。

4

1 回答 1

0

你应该打电话

speak(readableMessage);

仅在调用 OnInit 之后。通过在 Onstart 中调用它,您无法保证这一点。这就是当你回到应用程序时它被初始化并且你能听到的原因

于 2012-08-17T13:12:16.957 回答