Пользовательский звук уведомления не воспроизводится

Я пытаюсь настроить воспроизведение звука в уведомлении в строке состояния. Файл .mp3 находится в папке res/raw/. Но когда я уведомляю пользователя, звук не воспроизводится. Я пробовал использовать MediaPlayer, и он работает, но я не хочу, чтобы он воспроизводился с помощью MediaPlayer.

Вот мой метод:

public void showNotification()
{
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.feedback;        // icon from resources
        CharSequence tickerText = mContext.getString(R.string.statusbar_notification); // ticker-text
        long when = System.currentTimeMillis();         // notification time
        Context context = getApplicationContext();      // application Context
        CharSequence contentTitle = mContext.getString(R.string.statusbar_notification);  // message title
        CharSequence contentText = mContext.getString(R.string.statusbar_notificatione_detailed);      // message text

        Intent notificationIntent = new Intent(mContext, Main.class);
        PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);

        // the next two lines initialize the Notification, using the configurations above
        Notification notification = new Notification(icon, tickerText, when);

        //notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.sound = Uri.parse("android.resource://" + getPackageName() + "/R.raw.notificationsound");

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        mNotificationManager.notify(1, notification);
}

Спасибо.


person rogcg    schedule 07.11.2011    source источник


Ответы (1)


Из документации для ContentResolver:

Uri должен быть в одном из следующих форматов: android.resource://package_name/id_number.

Вы передаете строку «R.raw.notificationsound», которая ничего не значит. Вместо этого попробуйте следующее:

notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notificationsound );
person xorgate    schedule 07.11.2011
comment
как установить продолжительность цикла для этих звуков уведомлений... как повторить шаблон на этих - person Senthil Mg; 12.04.2013