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

馋奶兔 提交于 2019-12-24 09:55:00

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/39876885/android-media-ringtone-play-stops-working-after-a-28-plays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!