Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing video when activity is hidden

An example of this is most easily understood by thinking of "Pandora for Android".

My activity has a VideoView playing lectures from a class. I would like to be able to switch from this Activity to another activity (In my application, or to a different application (home screen, some other app) ) without interruption.

Using the Pandora example I could go to the 'switch radio stations' view while playing music and I could go to the home screen and continue navigating on the phone seamlessly.

Is this possible for videos, or audio only? Can I just play the audio from a video file and then switch back easily?

like image 396
steve-gregory Avatar asked Jun 28 '11 19:06

steve-gregory


1 Answers

The general way this is handled (and how Pandora handles it) is to playback the music from a service on a background thread. VideoView can't be used in this case as it's a View and is tied to an activity.

I would approach this by starting with the Android VideoView code and splitting it in two, one part to handle the UI and the other for video playback.

Run the MediaPlayer in a background service and when your video activity is shown, have the service call MediaPlayer.setDisplay() with your video view surface, and call MediaPlayer.setDisplay() with null when your video view is hidden.

Obviously this is much more complex than just using VideoView directly and requires starting a service and coordinating that with the UI. You could also probably get away with just running the MediaPlayer in a background thread and not a service.

like image 92
dhaag23 Avatar answered Oct 06 '22 18:10

dhaag23