Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaPlayer in separate Thread vs running in Service via startForeground()

Tags:

android

So, I'm streaming music in a separate thread. If I leave the app (onPause(), onStop() getting called, etc), the music continues to play, but eventually -- after opening other apps and switching between them and going back to the home screen -- my app is killed. No crash, just a WIN DEATH and process com.myapp.android has died in logcat. Obviously, it's legit for apps to be destroyed by the system in order to reclaim resources.

My question is: does running stuff in a thread off of the main (UI) thread mean that it now has less priority as far as the system is concerned? Meaning, is it more likely to be killed than if I run the media player in a Service and even use startForeground() to make the service run in the foreground?

Any thoughts or clarification would be greatly appreciated!

EDIT

Also, part of the documentation on Services confuses me. In pertinent part, it states:

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work.

I've always ran MP3 playback in a service on the main thread and the UI remained responsive. If I'm supposed to put it in a separate thread as recommended in the above quotation, then won't I end up back where I started, namely with the media playback occuring off of the main thread, thereby increasing the likelihood that the playback is killed when other apps are opened, etc?

like image 508
LuxuryMode Avatar asked Jan 18 '12 18:01

LuxuryMode


People also ask

Which service run on a separate worker thread?

IntentService. Like Service , IntentService runs on a separate thread, and stops itself automatically after it completes its work. IntentService is usually used for short tasks that don't need to be attached to any UI.

Does by default service runs on a separate thread?

Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.

What does MediaPlayer release do?

MediaPlayer class can be used to control playback of audio/video files and streams.

What is the use of MediaPlayer class?

One of the most important components of the media framework is the MediaPlayer class. An object of this class can fetch, decode, and play both audio and video with minimal setup. It supports several different media sources such as: Local resources.


2 Answers

I just posted part of this answer elsewhere, but it's still relevant.

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.

Ideally, you should be spawning a thread that controls your MediaPlayer from a Service. That way you ensure your media continues to play while your app is in the background and any blocking calls (aside from prepare()/prepareAsync()) do not hang your app.

like image 87
ajacian81 Avatar answered Jan 11 '23 00:01

ajacian81


I think most of your question is answered in the Android documentation on Services.

From the link:

A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

To answer your question, yes, running a separate Thread off of an Activity is going to have lower priority than a Service running in the foreground, if the Activity that started the Thread has been stopped/paused.

Personally, I would recommend doing this in a Service, as this is exactly the sort of thing the Service class has been created for.

Edit:

Interesting, what you suggest seems contrary to what I have read (everything is responsive when not using thread in Service). Regardless, it probably should be started in a background thread.

The thread (a thread that YOU create) won't die unless the process dies, and that will happen when the runtime decides that resources are needed. Activity's can be killed when they are not in the foreground, so their Thread's can be destroyed too. If you have a Service marked as foreground, it is less likely to be destroyed, and therefore, you should not have the same problem.

like image 41
nicholas.hauschild Avatar answered Jan 11 '23 02:01

nicholas.hauschild