【问题标题】:Custom notification sound not playing自定义通知声音未播放
【发布时间】:2011-11-07 22:37:57
【问题描述】:

我正在尝试在状态栏通知上播放自定义声音。 .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);
}

谢谢。

【问题讨论】:

    标签: android android-notifications android-notification-bar


    【解决方案1】:

    来自 ContentResolver 的文档:

    Uri 应该是以下格式之一: android.resource://package_name/id_number

    您传递的字符串“R.raw.notificationsound”没有任何意义。 试试这个:

    notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notificationsound );
    

    【讨论】:

    • 我如何设置这些通知声音的持续时间周期...如何在这些上重复模式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 2017-01-16
    • 1970-01-01
    相关资源
    最近更新 更多