Why should service be used for playing background music in Android? Why not I just create another media player object loop the player to play the music?
Thanks!
Keyword is background. If you tie your Media Player object to an activity, you don't have the guarantee that your object will be around if your activity goes to the background. The GC will destroy it when resources become low.
Therefore using a Service is safer. And services are designed for running in the background unlike activities.
Related
I initialize a MediaPlayer object in my main activity to play music. I would like my app to keep playing music when the user closes the app. I guess when the user closes the app, the MediaPlayer object goes out of scope. How can I keep keep the music playing after the app is closed?
How extend life cycle of an activity object
You can't.
If you want to run code when your app is in the background, you have to use a Service
You're looking at this wrong. Extending the life cycle means opening the door to leaks. There's also a considerable amount of things to keep track of.
The recommended way is via a Foreground Service. Your activity ideally binds to your service and controls the music playback via the provided Binder interface.
You can also allow the service to be controlled directly through your service's Ongoing Notification.
An example of the the above is Google Play Music.
Good luck.
I am building a music player in android for which I am using bound services to create mediaplayer and do all the background operations like playing media and pause etc like this.
All the UI components in mediaplayer are implemented in activity and I need to communicate events like mediaplayer is ready, media is playing, paused etc from service to activity to make changes in UI accordingly. What would be best way to do that?
Broadcasts from service to activity for various states is one way to implement it.
I have a question about android MediaPlayer class
As I've seen that MediaPlayer by default continues to run when the user switches to background mode.
But what I have seen here Media Playback .. They stated that if we want to make MediaPlayer plays in background, we must use Services.
I want to know why I should use services if it is already playing in background
Best regards
Activities are very fickle, if android runs out of memory it will start to kill off backgrounded tasks/activities with onDestroy(). Once that method is called your mediaPlayer will be destroyed and will stop playing. Services, however, are given a higher priority than backgrounded activities and android will avoid destroying them for as long as it can, probably never.
I am using MediaPlayer to play radio stream. The problem occurs when I background the main activity which plays the stream.
When the phone uses too much resources (for example while trying to display a list of installed applications) the stream stopps. I suspect, that Android shuts down the stream in order to save up some resources.
This is how I prepare the mediaplayer when I start:
radioPlayer=new MediaPlayer();
radioPlayer.setDataSource(streamLocation); //with a try/catch of course in the full code
radioPlayer.prepareAsync();
radioPlayer.seekTo(0);
radioPlayer.setOnInfoListener(this);
radioPlayer.setOnPreparedListener(new OnPreparedListener(){
public void onPrepared(MediaPlayer mp)
radioPlayer.start();
});
It is all running on the main thread.
A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
Dive into deep
Try to have the mediaplayer run as a service instead of within the activity. That way, when your app goes into suspend, you can have mediaplayer play in the background. When the user breaks focus away from the app (for instance, if a phone call comes in) the activity will suspend or destroy itself. However, services are designed to run in the background alongside everything that is running in the Android OS.
Services
I am writing an app that uses a media player. I want to start the media player using the service so that the music can be played even if the application is closed (onDestroy is run). Should I use BindService (for control the service) and not unBinding that?
the life cycle of the Service would be a bit tricky, start at playing screen created and stopped when either the music is complete in background or music is stopped when the app is finished (onDestroy)
how should i implement my service to best fit the above case i need?
Should I use BindService (for control the service) and not unBinding that?
No. You should call startService() to start it and stopService() to stop it (e.g., when the user presses the Stop button).