Android Notification Manager播放错误的声音,总是第一个播放

问题描述

我的应用程序的一部分是计时器倒数工具,用户可以在其中选择要通知的计时器和自定义声音。有10种不同的声音,从“ alarm1.mp3”到“ alarm10.mp3”,都在原始资源文件夹中。

我在延迟后处理程序的情况下启动通知

long delay = 15000; //some delay value chosen by user
int sound_id = 4; //some sound id value chosen by user (1-10)
final Runnable r = () -> {
    NotificationClass notification = new NotificationClass();
    String text = AppLib.getStr(R.string.CountdownCompletealert);
    notification.sendAlarmNotification(text,sound_id);
};
final Handler handler = new Handler();
handler.postDelayed(r,delay);
finish();

在NotificatonClass中:

public class NotificationClass {
    private final String ALARM_ID = "Job Manager Alarms";
   
    public NotificationClass() { }
    
    @RequiresApi(api = Build.VERSION_CODES.O)
    public void sendAlarmNotification(String ticker,int sound_id) {
        Context context = AppLib.getContext();
        notificationmanager notificationmanager = AppLib.getContext().getSystemService(notificationmanager.class);
    
        if (notificationmanager != null) {
            List<NotificationChannel> channelList = notificationmanager.getNotificationChannels();
            for (int i = 0; channelList != null && i < channelList.size(); i++) {
                notificationmanager.deleteNotificationChannel(channelList.get(i).getId());
            }
            CharSequence name = "JobManagerAlarm";
            NotificationChannel mChannel = new NotificationChannel(ALARM_ID,name,notificationmanager.IMPORTANCE_DEFAULT);
            AudioAttributes attributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();
            String sound = "alarm" + sound_id;
            int res_sound_id = context.getResources().getIdentifier(s,"raw",context.getPackageName());
            Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + res_sound_id);
    
            mChannel.setDescription("");
            mChannel.enableLights(true);
            mChannel.enableVibration(true);
            mChannel.setSound(sound,attributes);
            notificationmanager.createNotificationChannel(mChannel);
    
            Intent intent = new Intent(context,AlarmTimer.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context,intent,PendingIntent.FLAG_ONE_SHOT);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context,ALARM_ID)
                    .setSmallIcon(R.drawable.vector_notif)
                    .setTicker(ticker)
                    .setContentTitle(ticker)
                    .setContentText("")
                    .setColor(AppLib.getAppColour(R.color.rcOutcome))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(ticker))
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true);
            notificationmanagerCompat notificationmanagerCompat = notificationmanagerCompat.from(context);
            notificationmanagerCompat.notify(G.simpleMessageCounter,builder.build());
            G.simpleMessageCounter++;
        }
    }
}

第一次发出通知时,我听到正确的声音。下次,无论我选择哪种声音,都会再次播放第一个声音。试图重新启动设备,仍然是相同的声音。然后,我尝试将文件(正在播放alarm9.mp3文件重命名为alarm09.mp3。当收到新通知时,我听到了新声音,该声音是从用户中选择的。但是,此后,所选文件将变为“认”文件,并且无论用户选择如何,该文件始终会播放。

我什至尝试在创建新频道之前删除所有频道。只是不知道该怎么办。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)