A (175)

(Tuis.) #1

434 CHAPTER 11: Digital Video: Streaming Video, MediaPlayer, and MediaController classes


Let’s get back into Eclipse ADT and finish writing the Java code needed to implement this video
asset using only a VideoView widget inside of a FrameLayout container in the PlayVideo Activity
subclass in your Android app. I am going to show you how to use the .start( ) method before we get
into the MediaPlayer class, so that you understand that you can implement video in Android (as an
Absolute Beginner) without any knowledge of, or direct usage of, the MediaPlayer class or its related
classes whatsoever! I am making sure to go from easy to difficult during this chapter, as using digital
video assets in Android can range from simple to complicated.


Starting A Video Playback: Using .start( )


Now that we have learned how to actually develop a video asset for use in your digital video
UI designs within Android, we can finish writing the Java code which you have been writing to
implement a digital video in your application’s PlayVideo.java Activity subclass. The only line of Java
code that we have not yet put into place is the call to the .start( ) method, which we will make off of
the videoPlayer VideoView object, which you’ve already loaded with a URI for the /res/raw/flyover.mp4
video asset. We accomplished this using the Uri.parse( ) Uri class and the .parse( ) method. A call to
this .start( ) method off of the videoPlayer VideoView object is done using the following simple line of
Java code (more of a Java statement, actually) which is seen in Figure 11-28:


videoPlayer.start();


As you can see in Figure 11-28, you have implemented digital video playback in your Java code
in a half-dozen lines of Java code, not including import statements, of course. Since we used a
couple different emulators in the previous chapter, we can reset our Eclipse environment back to
using the Nexus One emulator using a Run As ➤ Configurations menu sequence to open the
Configurations Editor, and selecting and setting a Nexus One AVD for use.


Before we use the Run As ➤ Android Application menu sequence to launch the AVD, we will need
to add the PlayVideo Activity declaration into our AndroidManifest.xml file, so that when the Intent
object calls it, the Android OS knows that it is there.


This is because the PlayVideo Activity has been declared (and has been setup by the Android OS)
for use in the application’s Manifest. Copy the EditGalaxy tag structure in the Manifest,
and then paste it underneath itself. Change EditGalaxy to PlayVideo, and edit_galaxy to play_video,
Save the file, and you’ll be ready to fly over your planet’s surface!

Free download pdf