Android - Ringtone wont stop playing
Asked Answered
P

1

6

I currently start a Ringtone on Android and it plays fine.

However when I try to stop the ringtone it doesn't stop or atleast doesn't stop straight away, it will keep in playing until it just plays out.

Here is how I set up the Ringtone:

int rm = audio_service.getRingerMode();
int vs = audio_service.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER);

android.os.Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

if ((rm == AudioManager.RINGER_MODE_VIBRATE ||
    (rm == AudioManager.RINGER_MODE_NORMAL && vs == AudioManager.VIBRATE_SETTING_ON)))
            v.vibrate(vibratePattern,1);

if (audio_service.getStreamVolume(AudioManager.STREAM_RING) > 0) {                          

    oRingtone = RingtoneManager.getRingtone(this, Settings.System.DEFAULT_RINGTONE_URI);
    oRingtone.setStreamType(AudioManager.STREAM_RING);
    oRingtone.play();       

    }

And here is how I try to stop it

if (CallDialogActivity.oRingtone != null) {
      Log.d("RINGTONE", "Into Ringtone if");
      Ringtone ringtone = CallDialogActivity.oRingtone;
      oRingtone = null;
      ringtone.stop();
}

Has anyone come across similiar problems or see any mistakes in my code?

EDIT:

Just to add I have added logging to see if ringtone.isPlaying() returns true or false after I call ringtone.stop() and it returns false, however the tone keeps playing for a couple of seconds (3 - 5 seconds) after before stopping

Pooka answered 26/2, 2010 at 15:6 Comment(1)
i think it's better if you stop the ringtone first before setting it to null. ringtone.stop() actually works for me.Baltazar
P
6

For anyone that comes across this:

I had to use the RingTone manager to help stop the ringtone.

Adding the following line sorted it out

ringtoneManager.stopPreviousRingtone();
Pooka answered 11/3, 2010 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.