How to pause and resume application? - android

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

Related

Continuous playing of music when re-creating an Activity?

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

Android Stop background music when Home key pressed

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.

Android - Paused music automatically restarts to play after answering a phone call

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

How to make MediaPlayer play music only while my app is in foreground?

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.

Activity which downloads data in background with service and doesn't get destroyed on hitting back button

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 .

Categories

Resources