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
Related
I have made a simple application which plays video in Video-view. Now i want my application to run when my phone is idle as in when no activity is running and to pause when some other activity runs.
So what I have understood from your question is you want to pause video playback when some other app comes to foreground.
The simplest answer is onPause() method of an activity.
When another activity comes to foreground, your activity goes in pause state, that's where you can write the code for stopping video playback, save the current instance (time) at which the video is running and when your activity again comes to foreground, restore that data and start playing the video from the same position it was paused.
For restoring the data when the activity restores, you can write the code in onResume().
Hope this helps
I am developing an app which has multiple activities. User can navigate to any activity. I start background music from first main activity and it keeps playing throughout the application. Now I want that whenever user presses HOME key, the media player should pause playing and when user comes back to app, it starts playing again. First I made media player static and was pausing music in onPause() and playing in onResume() but it creates a jerk while switching between activities. I hope you got my point. Any idea how to pause playing when HOME key pressed and play it again when user comes back?
Take a look at Activity.onUserLeaveHint()
http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
Called as part of the activity lifecycle when an activity is about to
go into the background as the result of user choice. For example, when
the user presses the Home key, onUserLeaveHint() will be called, but
when an incoming phone call causes the in-call Activity to be
automatically brought to the foreground, onUserLeaveHint() will not be
called on the activity being interrupted. In cases when it is invoked,
this method is called right before the activity's onPause() callback.
I had the same issue and solved it in an ugly way: there is a global static player. in the onPause of an activity it calls the player to stop (but it does not actually stop), and in onResume call to start.
In the player onStart I mark the music as playing. In onStop, I mark the music as stopped but not actually stop it. I wake up 1 second after stop was called , and if there was no "startPlaying" in the last second, I stop the music.
I hope there is a better way.
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.
I have written a Music player app and it works great but when a flip action happens or when I return the the player view I have to stop the player and restart it at the postion that it was at when the action happened. That all works but it means a breif stop and start.
How can I run the media player in a different thread and still update my seekbar?
Thanks
Try running the media player within a foreground service. It's a little bit of work, but it's how it should be done anyways.
The service will handle running everything in a background thread and can be set up to post updates to your UI through messages or callbacks.
In addition, a Foreground service does not need to be tied to an activity, so it can continue to run even if you leave your player screen. The service must provide an Ongoing notification to the user that will probably display the current song, artist, click to pause, etc.