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?
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.