Should a MediaPlayer run in separate thread?

后端 未结 3 1958
花落未央
花落未央 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:04

    Could the streaming music be causing the main thread to stop until the music has finished streaming? This could be why it is slowing it right down.

    I'm no expert and am currently learning myself, but it's worth thinking about.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • No if you are doing any network transfer you should keep that in a thread, mediaplayer is not that resource intensive. Keep it on your activity.

    0 讨论(0)
提交回复
热议问题