android.media.Ringtone.play() stops working after a 28 plays
Asked Answered
V

1

2

I have an app that is open for hours and uses a background service with foreground notification attached to it. Every once in a while a sound is played using:

try {
    Ringtone r = RingtoneManager.getRingtone(context, uri);
    r.play();
} catch (Exception e) {
    e.printStackTrace();
}

The sounds are working, but after a while the sounds don't play anymore.
No errors, no warnings, no crashes. Just no sound.

My users are complaining as well, so it doesn't look like a device specific issue.

Android Docs don't mention anything about this. Anyone knows why this is?

Valeryvalerye answered 5/10, 2016 at 14:35 Comment(2)
Have you tried using MediaPlayer instead of Ringtone? Something like here: #12833134Toxemia
Yes I have, see my answer for my solution.Valeryvalerye
V
5

Don't create a Ringtone or MediaPlayer in a BroadcastReceiver!

The problem was the context of the Ringtone or MediaPlayer. By triggering the sound form a broadcast receiver a new MediaPlayer or Ringtone was created every time the sound played. After 28 times, the sound just didn't play anymore.

By playing the sound from an IntentSerice, or re-useing a static instance of the MediaPlayer or Ringtone, everything played as expected.

Valeryvalerye answered 1/2, 2017 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.