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.
Related
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
I have an application that sets a rtsp stream of a song. When I push the "play" button on the application, the music starts playing. Then, when I push the "pause" button, the music is paused.
However, while the music is paused, if I receive a phone call, answer it, and then finish the call, the paused music AUTOMATICALLY restarts to play!! I DON'T want that to happen (because the user left the music paused)! I want the music to remain paused.
How do I fix this? I guess it's an Android problem!
PS: Just as a note, the oppose situation works fine, that is, if I receive a call while a music is being played, the Android does the right thing: it pauses the music for me to answer the call and, after the call is finished, the music starts to play again from where it was.
Your app has an implementation of PhoneStateListener somewhere that pauses the music when a call begins and restarts it when the call ends. You need to save the current state of the music (paused or playing) when a call begins and when the call ends, only start playing the music if it was playing when the call started. It's difficult to be more specific without your code but this question has an example implementation.
Could probably use more info, but just adding your pauseMusic() function in the activity's onPause() event should do the trick
I am building an android game which consists of several activities. While the app is running some background music is playing. The background music consists of several loops which are played one after another.
So my problem is how to detect when to stop the background music. The music should stop if the user exits the app, turns off the screen or switches to another app and it should be turned back on when the user returns to my game.
My current approach is to notify a SoundManager class which keeps track on the active activity. Every time a new activity starts (onResume) and every time an activity is ended (onStop) the SoundManager increases an internal counter. If the counter is 0 the sound is stopped and if the counter is > 0 the sound is started (if it is not already playing).
Additionally the SoundManager listenes for ACTION_SCREEN_OFF / ACTION_SCREEN_ON events to turn the sound off/on depending on the screen state.
This approach works quite well except for one case: If I turn off the screen sometimes (I'd say with a 50% chance) the sound starts again because apparently onResume is invoked on the activity which was active before the screen was turned off. I wonder why that happens.
So my questions are:
- Is there is a less fragile approach to know when to stop the background sound?
- or is there is a workaround for the screen on/off issue.
Thanks for your help!
I've encountered such problem when I tried to show rating dialog on the 20th activization of application. Unfortunately, Android hasn't iOS-like "applicationDidEnterBackground", so I've tried this way.
I extended Application class and added methods onResume() and onPause() (for application)
In every activity I've overrided method onKeyDown() and when KeyEvent was "Home", I've called method onPause() of custom application class. In method onPostResume of every activity I've checked, was application visible (by flag) or not, if yes, I've called method onResume() of custom application class.
As I can see, your method is better. But maybe it will help you or give any clue.
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.
I need to develop an application with downloads the data at the background and update about the progress in the front.
I guess this can be achieved using services and activity and passing data in between the. But, what I need to do is even if I hit back button and then start the activity again. It should check if the service is running or not. If service is not running it should start one else it should display the data from running service.
Something like music player where music is played by a service at a background and activity displays the information. Even on browsing through other activities of the application or hitting back, state of the music player is maintained.
Cheers,
Prateek
Well Prateek,
Service can be started at any time you set & can be destroyed after you complete the task.
For further help have a look # http://developer.android.com/reference/android/app/Service.html#WhatIsAService .