Do I really have to watch that video? Playing videos in a service to enable backgrounding of the video and extending the lifecycle of it beyond that of a single activity. Chris Doyle – Android Software Engineer [email protected]
1. Do I really have to watch that video? Playing videos in a
service to enable backgrounding of the video and extending the
lifecycle of it beyond that of a single activity.
2. We're gonna have a bouncing electro-booty contest
3. I got one less problem without ya We want to show video ads
to our free users We dont want to spoil our user experience by
making them watch the damn things
4. And I will try to fix you Allow users to stop watching the
video and have the audio continue Control the playback of the video
using a service
5. Show Em Whatcha Got
6. Theres advantage to each, advantages, advantages Better
control of the lifecycle of video playback Span playback across
multiple activities Accommodate backgrounding and foregrounding of
the video Dont let the video with the activity
7. This is how we do it Originally implemented using
MediaPlayer Recently migrated to Exoplayer
8. Stop, collaborate and listen
https://github.com/doyley2000/android-video-service
9. System, design fi set we up
10. a sample in a jar
11. Piece of the action, yeeeaah LOAD Prepare the video before
playing it Load the VCR into the deck Video Service
12. Piece of the action, yeeeaah START Start playback Be
careful not to start before the tape is in the player! We can
choose to do this without the TV on Video Service
13. Piece of the action, yeeeaah RESUME VIEWING Turn the TV on!
Video Service
14. Seeing is deceiving, dreaming is believing
15. private void initialise(Uri videoUri) { // initialise
exoplayer, create sample source and renderers
mExoPlayer.prepare(mVideoTrackRenderer, mAudioTrackRenderer);
mExoPlayer.setRendererEnabled(TYPE_VIDEO, false);
mExoPlayer.setRendererEnabled(TYPE_AUDIO, true); } Fade into the
background 1. Start with the video renderer disabled
16. private SurfaceHolder.Callback mSurfaceCallback = new
SurfaceHolder.Callback() { @Override public void
surfaceCreated(SurfaceHolder holder) {
mVideoService.setBackgrounded(false, holder.getSurface()); } } Fade
into the background 2. Use the SurfaceHolder or TextureView
callbacks to know when to show/hide the video
17. private SurfaceHolder.Callback mSurfaceCallback = new
SurfaceHolder.Callback() { @Override public void
surfaceDestroyed(SurfaceHolder holder) {
mVideoService.setBackgrounded(true, null); } } Fade into the
background 3. Background the video when the surface is
destroyed
18. public void setBackgrounded(boolean backgrounded, Surface
surface) { if (backgrounded) { // null the surface
mExoPlayer.blockingSendMessage(mVideoTrackRenderer,
MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, null); // disable the
video renderer mExoPlayer.setRendererEnabled(TYPE_VIDEO, false);
Fade into the background 4. Two things to perform when sending into
the background:
19. } else { // set the surface
mExoPlayer.sendMessage(mVideoTrackRenderer,
MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, surface); // enable
the video renderer mExoPlayer.setRendererEnabled(TYPE_VIDEO, true);
} Fade into the background 5. And conversely when the video is
coming into foreground
20. Maintain the same surface across different activities Where
do we go now? Extend the VideoPlayerActivity to be used for
different activities Handle adaptive streaming using DASH or HLS
Play multiple videos at once
21. Thats the gist, thats the summary Complex video
implementations can be tricky Exoplayer makes life easier
Backgrounding can be easy if implemented correctly To get really
fancy, use a texture view and dont let your surface be
destroyed