Reputation: 11
I am building an app for self use only - on the basis of sunrise and sunset different alarms are set (Muslim namaz times)
For alarm I have used Timer, which keeps checking device time with alarm time; if both are same the alarm plays.
But I am not able to stop alarm after tapping on notification.
onResume
I have tried mediaplayer.stop()
, but it's playing media for multiple times.
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();
}
}
Upvotes: 1
Views: 101
Reputation: 997
void playSounds( int i ){
String message = "Stop";
//1=Azzan; 0 = Alarm
if(i == 0)
{
if( mediaPlayer != null )
{
mediaPlayer.reset();
mediaPlayer.release();
}
....
....
continue with your stuff
you can use,
mediaPlayer.reset();
Upvotes: 1