I currently have an activity that plays music. When the activity is destroyed (i.e. user rotates the screen) I have the application store the music player's state and then play the music from the exact spot in the song once the activity is re-created.
My only issue is that there is a noticeable pause in the music when the activity is re-created. Any ideas on how to increase the activity's performance in regards to re-creating the app more efficiently. I know I can stop the activity to not re-create objects by using the configChanges tag in the android manifest but I would like to avoid that.
Is running the music in a separate service my only option?
Since the activity is destroyed it will pause no matter what you do. You should use a Service for this purpose. A service runs on a separate thread and is not effected by your activity. There's loads of tutorials and stackoverflow questions for services
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 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 read Anndroid document - http://developer.android.com/guide/topics/media/mediaplayer.html
which said ".. you want it to continue playing while the user is interacting with other applications—then you must start a Service and control the MediaPlayer instance from there. "
But I found if I start a local a music file from an activity, then leave that activity, ( for example, press HOME key and interact with another app ), the music continues playing.
So, I don't understand " the "must start a Service" in document. Did I miss something?
This was not a big obstacle for my app at this moment. I am just wondering what's the potential problems could be if I do not use Service.( Services have longer lifespan, so the mediaplayer could be killed earlier, any others ? )
Our development is based on Android 2.2.
Thanks for any help in advance.
Big reason, if you are not using a service, users cannot listen to music outside of your app when the the activity gets paused or terminated. Technically you can make a music app but if your users cant listen to music when another app is in the foreground or the phone is in a different state(locked) it wont make for a very good app. You should take a look at the activity lifecycle for a deeper understanding of the process. Note that this behavior is by design for saving power, memory and cpu cycles.
It helps also not to think of services in the more traditional desktop dev usage. You want this thing to live even when your activity is not up and about.
For more about activity life cycles Managing the Activity Lifecycle
For the How
http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-iii-android-services/
For the why
Why is it important to use Services for background tasks?
Playing the music in the Activity might be fine for now, but when Android is low on resources it might try to kill your Activity. When you add a service to an app, Android will try to keep that process alive as long as possible if it falls under certain criteria (such as playing music). Read over the Process Lifecycle section on Services:
http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle
The activity can get killed by Android or by the user and then the music would stop playing.
I guess I still need to learn how Android apps flow. The title might not have been clear, so let me explain.
Situation:
I have a game which has a few different activities. For example, MenuActivity, GameActivity, and HowToActivity. The game starts at MenuActivity and plays a song set to loop. To have the same song play during MenuActivity and HowToActivity, uniterrupted, I have the song played from an implemented Application. If I press the home button, get a phone, or whatever, the song will continue to play. To prevent that, I need to stop the song when leaving the app.
Problem:
Currently, in MenuActivity, I have code to stop the song under the protected void onStop() function. This stops the song when leaving the app (Pressing the Home button, get a phone call), but it also stops the song when changing to another activity within the app, such as HowToActivity. So the question is, how can I tell the difference?
Jesse,
You need to have a service that will do the job of playing the song.
You can easily start the service from any of the activity of your application. Also the service can be stopped by any of the activity.
Hence in the activity onCreate(), you can start the song player service, that will play the song even if the activity dies and new activity starts. Once your application is done with the song playing, just call stopService().
I hope this will solve your issue.
~Rajan
Typically what happens is that people read the phone state using a PhoneStateListener:
http://developer.android.com/reference/android/telephony/PhoneStateListener.html
This is why so many apps need the READ_PHONE_STATE permission, they're making sure you aren't answering a call while the app goes off and continues to do something annoying. You can create a listener to check when things like this happen. You shouldn't really change the behavior of the home key (and can't!), but instead, you can always listen for things like onPause() and onStop().
You probably want a background service that actually does the music playing, etc.., and then you want to control this service from your actual app when you get lifecycle events inside activities. This makes your app a bit more modular (i.e., the thing that it's doing semantically is control the sound, download the stream, whatever), because the Activities control the UI, and the Services what happens behind the scenes.
Edit: tutorial for MediaPlayer from a service:
http://marakana.com/forums/android/examples/60.html
You might also want to look into using a wake lock, though it might not be strictly necessary.
Create a receiver to capture the following intent:
Intent.ACTION_CLOSE_SYSTEM_DIALOGS
This will be called when the Home screen of the phone is launched. So you can stop music at that time rather than stopping onStop() of Activity.
But this will not help if the user launch an app by pressing Home key long time.
So try to play different musics on different Screens.
I have an app consisting of several activities. I'd like a background music to play continuously while user is navigating between them and stop is user goes to home screen/some other app/locks the screen.
What is the best way to do this?
I ended up stopping music after 500ms timeout after onPause if it is not resumed in an onResume of some other activity. If activity switch takes longer than 500ms, then it's perfectly reasonable that music should stop until the next activity loads. And when user presses home button, 500ms delay before stopping music is not noticeable.
You can use a service to do the same. It is the best way to tun background tasks
Here is a sample service
http://androidcore.com/android-programming-tutorials/638-how-to-use-android-services.html
You can try adding mediaplayer related code here
Implement onPause and onResume. These are called by Android when your activity is sent to the background and when it comes back to the foreground. Use onStop to handle when your app is killed by Android and no longer visible or running.