410 CHAPTER 11: Digital Video: Streaming Video, MediaPlayer, and MediaController classes
When you implement a VideoView widget, you are also instantiating a MediaPlayer object, even
though you do not have to write any XML markup, or even write any Java code, to create this
MediaPlayer object! MediaPlayer objects are essentially video playback engines, and will play digital
video assets associated with the VideoView UI element. This is done using a URI object, and a video
asset reference, which the URI object contains. We’ll cover URI and MediaPlayer classes later on in
this chapter. The video playback states are shown in Table 11-2.
Table 11-2. Video Playback States and How They Affect the Android MediaPlayer Object and Its Video Playback
Video Playback State What is happening with the MediaPlayer object (video playback)
Idle State MediaPlayer object is instantiated and ready for configuration
Initialized State MediaPlayer object is initialized with data path, using a URI
Prepared State MediaPlayer object is configured, or “prepared,” for playback
Started State MediaPlayer object is started and is decoding the video stream
Paused State MediaPlayer object is paused and stops decoding video frames
Stopped State MediaPlayer object is stopped and stops decoding video frames
Playback Completed MediaPlayer object is finished decoding the video data stream
Ended State MediaPlayer object is ended and removed from system memory
I’ll go through these eight states in the logical order in which they are used and in the order in which
they are listed in Table 11-2.
- When your MediaPlayer object is first instantiated, it’s not actually doing
anything. A MediaPlayer object would therefore initially be in what is termed
the “Idle” state, much like a car idles when not “engaged.” - Once you load your MediaPlayer object with the video data reference using
your URI object, with a Uri.parse( ) method call or a .setDataSource( )
method call, a MediaPlayer object will enter what is termed an “Initialized”
state. There is an interim state, between the Initialized MediaPlayer object
state and the “Started” MediaPlayer object state, which is called the
“Prepared” MediaPlayer object state. - The Prepared state is accessed using the MediaPlayer.OnPreparedListener,
which is a nested class that we will be learning about in the MediaPlayer
sections of this chapter, and which we will be using a bit later on inside of our
HelloUniverse application Java code in the PlayVideo.java Activity subclass,
which will play a video asset. - Once the MediaPlayer object has been Initialized, and loaded with video
data, it is usually “prepared” (that is, configured using various playback
option settings). After this is done, a MediaPlayer object can then be Started.