当我从通知切换回应用程序时,为什么Media Player会多次播放媒体?

时间:2019-04-30 05:38:07

标签: android android-mediaplayer android-notifications

我正在构建仅用于自用的应用程序-根据日出和日落设置了不同的警报(穆斯林纳马兹时间)

对于警报,我使用了计时器,该计时器不断检查设备时间和警报时间。如果两者相同,警报会播放。

但是在点击通知后我无法停止闹钟。

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

1 个答案:

答案 0 :(得分:1)

 void playSounds( int i ){

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

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

继续您的东西

您可以使用

            mediaPlayer.reset();