CHAPTER 11: Digital Video: Streaming Video, MediaPlayer, and MediaController classes 445
The next thing that we will need to do is to “wire” the VideoView object to the MediaController object
so that they know each other are there and so they will work together seamlessly.
In pseudo code speak, we need to tell the MediaController object that it is controlling the VideoView
and tell the VideoView to use the MediaController object to control its media (in this case, digital
video). This “cross-wiring” of the two objects will take two lines of Java code, which after they are
in place, will give your user the ability to click (or touch) your video and bring up a MediaPlayer
Transport UI element.
The MediaController, or the MediaPlayer Transport, whichever way you want to look at it, will always
work regardless of whether you have your digital video asset set up to loop or to only play once.
- As you can see in Figure 1 1-3 8 we will first use the .setAnchorView( )
method call, off of the videoTransport object, to wire the videoTransport and
the videoPlayer objects together. This tells the videoTransport object: “the
videoPlayer VideoView object is your AnchorView.” - Then we will use the .setMediaController( ) method call, off of the
videoPlayer object, to wire the videoPlayer and the videoTransport objects
together. This will tell your videoPlayer object: “use the videoTransport
MediaController object as the MediaController for the VideoView.” These two
simple lines of Java programming logic should look like the following:
videoTransport.setAnchorView(videoPlayer);
videoPlayer.setMediaController(videoTransport);
- Use your Run As ➤ Android Application menu sequence to launch the
Nexus One AVD, and click the three dots at the top-right of your ActionBar
and select the Fly Over Planet option from the menu. Once your digital video
starts playing, even if it loops, at any time you can click on the screen and
bring up the video media transport controls.
After a few seconds, the transport controls will fade away if they are not being actively used to start,
stop, pause, or shuttle the digital video asset’s frames.
Congratulations, you have essentially mastered digital video for your Android application
development during this chapter, doing everything from learning the fundamentals of video to
creating and optimizing digital video assets to coding a video playback Activity with looping
capabilities and a transport controller UI element for your users to use. Pretty comprehensive for one
single chapter! If you want to venture more deeply into this subject, look for my titles Pro Android
Graphics (Apress, 2013) and Pro Android UI (Apress, 2014) that get into this subject area and
combine it with more advanced graphic design and user interface design topics.
Summary
In this chapter, you learned all about digital video concepts, formats, codecs, and principles,
expanding even further on the 2D animation concepts, formats, codecs, and principles that you
learned about in Chapter 10.