简体   繁体   English

自定义通知声音无法播放

[英]Custom notification sound not playing

I'm trying to make a custom sound play on a status bar notification. 我正在尝试在状态栏通知上进行自定义声音播放。 The .mp3 file is in res/raw/ . .mp3文件位于res / raw /中 But when I notify the user the sound is not played. 但是当我通知用户声音没有播放时。 I've tryied with MediaPlayer, and it works, but I dont want to make it play with MediaPlayer. 我已经尝试使用MediaPlayer,它可以工作,但我不想让它与MediaPlayer一起玩。

Here is my method: 这是我的方法:

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);
}

Thanks. 谢谢。

From the documentation for ContentResolver: 从ContentResolver的文档:

The Uri should be one of the following formats: android.resource://package_name/id_number Uri应该是以下格式之一:android.resource:// package_name / id_number

You are passing the String "R.raw.notificationsound" which means nothing. 你正在传递字符串“R.raw.notificationsound”,这意味着什么。 Instead try this: 而是试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM