MediaPlayer 在通知期间未完成并停止

MediaPlayer Doesn't finish and stops during Notification

我正在使用广播接收器在自定义时间发送通知。每次触发时,我都会使用 MediaPlayer 播放声音。问题是它有时会在播放声音的过程中突然停止。这是我的代码:

 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);

        @SuppressLint("InvalidWakeLockTag")
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TRAININGCOUNTDOWN");
        wl.acquire(10*60*1000L /*10 minutes*/);
        Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.icon);
            Intent inteent = new Intent(context, MainActivity.class);
            inteent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, xx, inteent, PendingIntent.FLAG_UPDATE_CURRENT);
        xx = xx + 1;

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                    .setSmallIcon(R.drawable.icon)
                    .setLargeIcon(icon)
                    .setContentTitle("بانگ")
                    .setContentText(notTitle)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    // Set the intent that will fire when the user taps the notification
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true);



        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
      // notificationId is a unique int for each notification that you must define


        // Put here YOUR code.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId("com.c4kurd.bang");
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    "com.c4kurd.bang",
                    "بانگ",
                    NotificationManager.IMPORTANCE_HIGH
            );
            channel.enableVibration(true);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }


        assert notificationManager != null;
        int id = context.getResources().getIdentifier(cc(prefs.getString("voices","")), "raw", context.getPackageName());
//                Toast.makeText(context, prefs.getString("voices",""),Toast.LENGTH_SHORT).show();
        MediaPlayer mp= MediaPlayer.create(context, id);

        notificationManager.notify(xx, builder.setVibrate(new long[]{1000,1000}).build());
        mp.start();
        wl.release();



    }

我不知道这个问题。是因为 mp.start() 在 Wake Lock 之前吗? 提前致谢。

DEFAULT:此行returns默认通知声音

Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

CUSTOM:这一行选择自定义通知声音: (将其放在 RAW(资源)文件夹中)

Uri notificationSound = Uri.parse("android.resource://"
            + context.getPackageName() + "/" + R.raw.my_custom_sound_file);

选择以上之一并将其添加到您的通知中: setSound(notificationSound)

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.drawable.icon)
                .setLargeIcon(icon)
                .setContentTitle("Your Title")
                .setContentText("Your Content")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent)
                .setSound(notificationSound)
                .setAutoCancel(true);

删除您使用自己的 MediaPlayer 的所有额外行。