Search code examples
androidandroid-studioandroid-layoutandroid-alarmsandroid-sound

Android studio Timer end Alarm sound playing problem with stop


I create a timer app and that code was play the alarm sound but how can I stop it? xD I use that code for playing:

Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone ringtoneSound = RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri)

if (ringtoneSound != null) {
    ringtoneSound.play();
}

So I want to click the reset button and its stop, its how possible? Many thanks, Dominik.


Solution

  • You can check first if the ringtone is playing, only then you stop it.

    if(ringtoneSound.isPlaying()){
            ringtoneSound.stop();
        }