Should a MediaPlayer run in separate thread?

后端 未结 3 1961
花落未央
花落未央 2020-12-15 21:52

I\'m building an app that streams music from a web server. The app has foreground service that uses a MediaPlayer for playback.

My code is based on this example: ht

3条回答
  •  遥遥无期
    2020-12-15 22:07

    Unfortunately, calling prepareAsync() is simply not good enough to avoid ANR prompts and your application hanging for a few seconds, especially if you're playing a file from the network. Your best bet is to put your MediaPlayer instance in its own thread, or at the very least execute intensive calls in a Handler (like mediaplayer.start()). I've been using MediaPlayer for over a year and I can tell you it definitely hangs after various calls, depending on the circumstances.

提交回复
热议问题