我想从资产文件夹设置推送通知声音,Android

I want to set Push Notification Sound from Asset Folder ,Android

我在 asset 文件夹中有一个名为 "error.mp3" 的 .mp3 文件。我想在推送通知的同时设置声音,我该怎么做,我已经尝试过这段代码来做到这一点

 Uri sound = Uri.parse("file:///android_asset/error.mp3");
 mBuilder.setSound(sound);

但它不起作用,当我使用默认通知声音时,它起作用了。我需要一些帮助...

这是我的 BroadcastReceiver class

public class Notificationmassage extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {


    showNotification(context);

}

private void showNotification(Context context) {
    Log.i("notification", "visible");



    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder =
            (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic)
                    .setContentTitle("Radio Planet")
                    .setContentText("Explore music");
    mBuilder.setContentIntent(contentIntent);


    mBuilder.setVibrate(new long[] { 500, 500});


    //Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    //mBuilder.setSound(alarmSound);



   Uri sound = Uri.parse("file:///android_asset/error.mp3");
   mBuilder.setSound(sound);

    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(2, mBuilder.build());


}
}

请将您的 .mp3 文件放入 raw 目录并尝试以下代码

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

请尝试将您的 .mp3 文件放在 raw 文件夹而不是 assets 中,有时它不起作用,所以请这样使用

Uri path = Uri.parse("android.resource://com.example.test/" + R.raw.sample);

com.example.test // 用你的包名替换这个

然后设置

mBuilder.setSound(path);

文件夹结构

res/raw/test.mp3

希望它对你有用。享受编程。

放在raw文件夹下使用

    Uri uri=Uri.parse("android.resource://"+getPackageName()+"/raw/notification_sound.mp3");