Выключение медиаплеера(mediaplayer) через уведомление, при срабатывании будильника и обновление уведомления после срабатывания

необходимо выключить медиаплеер через уведомление когда сработает будильник по таймеру и обновить значение случайного числа при этом не отменяя таймер. Очень буду признателен за помощь^^

Код MyServiceClass

public class MyService extends Service {

private Integer alarmHour;
private Integer alarmMinute;
private Ringtone ringtone;

private MediaPlayer mediaPlayer;


private Timer t = new Timer();
private AudioManager audioManager;


private static final String CHANNEL_ID = "MyNotificationChannelID";

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    alarmHour = intent.getIntExtra("alarmHour", 0);
    alarmMinute = intent.getIntExtra("alarmMinute", 0);


    String minute_string = String.valueOf(alarmMinute);
    if (alarmMinute < 10) {
        minute_string = "0" + String.valueOf(alarmMinute);

    }


    String time_to_wakeup = alarmHour.toString() + " : " + minute_string;

    mediaPlayer = MediaPlayer.create(this, R.raw.sound1);

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);



    Random random = new Random();
    final int n  = random.nextInt(20);



    try {


        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID )
                .setContentTitle("My Alarm clock")
                .setContentText("Alarm time - " + alarmHour.toString() + " : " + minute_string)
                .setSmallIcon(R.drawable.ic_stat_name)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification);

        NotificationChannel notificationChannel = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationChannel = new NotificationChannel(CHANNEL_ID, "My Alarm clock Service", NotificationManager.IMPORTANCE_DEFAULT);
        }
        NotificationManager notificationManager = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            notificationManager = getSystemService(NotificationManager.class);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationManager.createNotificationChannel(notificationChannel);
        }

    }
    catch (Exception e){
        e.printStackTrace();
    }

    t.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            if (Calendar.getInstance().getTime().getHours() == alarmHour &&
                    Calendar.getInstance().getTime().getMinutes() == alarmMinute){
                //mediaPlayer = MediaPlayer.create(this, R.raw.a1);



                AudioManager am =
                        (AudioManager) getSystemService(Context.AUDIO_SERVICE);

                am.setStreamVolume(
                        AudioManager.STREAM_MUSIC,
                        am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                        0);
                mediaPlayer.start();
            }
            else { }

        }
    }, 0, 2000);



    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {

    mediaPlayer.stop();

    super.onDestroy();
}

}


Ответы (0 шт):