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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With