首页 > 解决方案 > 当我从通知切换回应用程序时,为什么媒体播放器会多次播放媒体?

问题描述

我正在构建一个仅供自用的应用程序 - 根据日出和日落设置不同的警报(穆斯林 namaz 时间)

对于闹钟,我使用了 Timer,它不断检查设备时间和闹钟时间;如果两者相同,则播放警报。

但是点击通知后我无法停止警报。

onResume我试过mediaplayer.stop()了,但它会多次播放媒体。

void checkToPlaySounds(){
        cal = Calendar.getInstance();
        format = new SimpleDateFormat("HH:mm");
        currentTime = format.format(cal.getTime());
        dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

        if(mediaPlayer.isPlaying()){
            return;
        }
        if(currentTime.equals(fajr)){
            if(chkAzzanFajr.isChecked()){
                if(dayOfWeek==1 && chkAzzanSun.isChecked()){playSounds(1);}
                if(dayOfWeek==2 && chkAzzanMon.isChecked()){playSounds(1);}
                if(dayOfWeek==3 && chkAzzanTue.isChecked()){playSounds(1);}
                if(dayOfWeek==4 && chkAzzanWed.isChecked()){playSounds(1);}
                if(dayOfWeek==5 && chkAzzanThu.isChecked()){playSounds(1);}
                if(dayOfWeek==6 && chkAzzanFri.isChecked()){playSounds(1);}
                if(dayOfWeek==7 && chkAzzanSat.isChecked()){playSounds(1);}
            }
        }
        if(currentTime.equals(zohr)){
            if(chkAzzanZohr.isChecked()){
                if(dayOfWeek==1 && chkAzzanSun.isChecked()){playSounds(1);}
                if(dayOfWeek==2 && chkAzzanMon.isChecked()){playSounds(1);}
                if(dayOfWeek==3 && chkAzzanTue.isChecked()){playSounds(1);}
                if(dayOfWeek==4 && chkAzzanWed.isChecked()){playSounds(1);}
                if(dayOfWeek==5 && chkAzzanThu.isChecked()){playSounds(1);}
                if(dayOfWeek==6 && chkAzzanFri.isChecked()){playSounds(1);}
                if(dayOfWeek==7 && chkAzzanSat.isChecked()){playSounds(1);}
            }
        }
        if(currentTime.equals(magrib)){
            if(chkAzzanMaghrib.isChecked()){
                if(dayOfWeek==1 && chkAzzanSun.isChecked()){playSounds(1);}
                if(dayOfWeek==2 && chkAzzanMon.isChecked()){playSounds(1);}
                if(dayOfWeek==3 && chkAzzanTue.isChecked()){playSounds(1);}
                if(dayOfWeek==4 && chkAzzanWed.isChecked()){playSounds(1);}
                if(dayOfWeek==5 && chkAzzanThu.isChecked()){playSounds(1);}
                if(dayOfWeek==6 && chkAzzanFri.isChecked()){playSounds(1);}
                if(dayOfWeek==7 && chkAzzanSat.isChecked()){playSounds(1);}
            }
        }
        if(currentTime.equals(fajrReminder) && reminderMinutes!=0){
            if(chkAlarmFajr.isChecked()){
                if(dayOfWeek==1 && chkAlarmSun.isChecked()){playSounds(0);}
                if(dayOfWeek==2 && chkAlarmMon.isChecked()){playSounds(0);}
                if(dayOfWeek==3 && chkAlarmTue.isChecked()){playSounds(0);}
                if(dayOfWeek==4 && chkAlarmWed.isChecked()){playSounds(0);}
                if(dayOfWeek==5 && chkAlarmThu.isChecked()){playSounds(0);}
                if(dayOfWeek==6 && chkAlarmFri.isChecked()){playSounds(0);}
                if(dayOfWeek==7 && chkAlarmSat.isChecked()){playSounds(0);}
            }
        }
        if(currentTime.equals(zohrReminder) && reminderMinutes!=0){
            if(chkAlarmZohr.isChecked()){
                if(dayOfWeek==1 && chkAlarmSun.isChecked()){playSounds(0);}
                if(dayOfWeek==2 && chkAlarmMon.isChecked()){playSounds(0);}
                if(dayOfWeek==3 && chkAlarmTue.isChecked()){playSounds(0);}
                if(dayOfWeek==4 && chkAlarmWed.isChecked()){playSounds(0);}
                if(dayOfWeek==5 && chkAlarmThu.isChecked()){playSounds(0);}
                if(dayOfWeek==6 && chkAlarmFri.isChecked()){playSounds(0);}
                if(dayOfWeek==7 && chkAlarmSat.isChecked()){playSounds(0);}
            }
        }
        if(currentTime.equals(magribReminder) && reminderMinutes!=0){
            if(chkAlarmMaghrib.isChecked()){
                if(dayOfWeek==1 && chkAlarmSun.isChecked()){playSounds(0);}
                if(dayOfWeek==2 && chkAlarmMon.isChecked()){playSounds(0);}
                if(dayOfWeek==3 && chkAlarmTue.isChecked()){playSounds(0);}
                if(dayOfWeek==4 && chkAlarmWed.isChecked()){playSounds(0);}
                if(dayOfWeek==5 && chkAlarmThu.isChecked()){playSounds(0);}
                if(dayOfWeek==6 && chkAlarmFri.isChecked()){playSounds(0);}
                if(dayOfWeek==7 && chkAlarmSat.isChecked()){playSounds(0);}
            }
        }

        if(currentTime.equals(sioriStartReminder) && Integer.valueOf(edtSioriBeforeStart.getText().toString())!=0){
                if(dayOfWeek==1 && chkSioriSun.isChecked()){playSounds(0);}
                if(dayOfWeek==2 && chkSioriMon.isChecked()){playSounds(0);}
                if(dayOfWeek==3 && chkSioriTue.isChecked()){playSounds(0);}
                if(dayOfWeek==4 && chkSioriWed.isChecked()){playSounds(0);}
                if(dayOfWeek==5 && chkSioriThu.isChecked()){playSounds(0);}
                if(dayOfWeek==6 && chkSioriFri.isChecked()){playSounds(0);}
                if(dayOfWeek==7 && chkSioriSat.isChecked()){playSounds(0);}
        }
        if(currentTime.equals(sioriEndReminder) && Integer.valueOf(edtSioriBeforeStart.getText().toString())!=0){
            if(dayOfWeek==1 && chkSioriSun.isChecked()){playSounds(0);}
            if(dayOfWeek==2 && chkSioriMon.isChecked()){playSounds(0);}
            if(dayOfWeek==3 && chkSioriTue.isChecked()){playSounds(0);}
            if(dayOfWeek==4 && chkSioriWed.isChecked()){playSounds(0);}
            if(dayOfWeek==5 && chkSioriThu.isChecked()){playSounds(0);}
            if(dayOfWeek==6 && chkSioriFri.isChecked()){playSounds(0);}
            if(dayOfWeek==7 && chkSioriSat.isChecked()){playSounds(0);}
        }

    }
    void playSounds(int i){

        String message = "Stop";
        //1=Azzan; 0=Alarm
        if(i==0){
            mediaPlayer= MediaPlayer.create(MainActivity.this,R.raw.alram);
            mediaPlayer.setLooping(true);
            mediaPlayer.start();

            Intent activityIntent = new Intent(this, MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this,
                    0, activityIntent, 0);
            activityIntent.putExtra("stopMedia",true);

            Intent broadcastIntent = new Intent(this, NotificationReceiver.class);
            broadcastIntent.putExtra("toastMessage", message);
            PendingIntent actionIntent = PendingIntent.getBroadcast(this,
                    0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("Mumineen Alarm")
                    .setContentText("Namaz Reminder - Tap to Stop")
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setCategory(NotificationCompat.CATEGORY_ALARM)
                    .setColor(Color.BLUE)
                    .setContentIntent(contentIntent)
                    .setAutoCancel(true)
                    .setOnlyAlertOnce(true)
                    .build();

            notificationManager.notify(1, notification);
        }
        if(i==1){
            mediaPlayer= MediaPlayer.create(MainActivity.this,R.raw.azaan);
            mediaPlayer.start();
        }
}

标签: androidandroid-mediaplayerandroid-notifications

解决方案


 void playSounds( int i ){

    String message = "Stop";
    //1=Azzan; 0 = Alarm

    if(i == 0)
      {
         if( mediaPlayer != null )
            {
              mediaPlayer.reset();
              mediaPlayer.release();
            }
   ....
   ....

继续你的东西

您可以使用,

            mediaPlayer.reset();

推荐阅读