Beginning Android Development

3 downloads 5977 Views 2MB Size Report
Jan 22, 2014 ... Tampa Bay Android Developers Group ... See the Android developer reference for a ... The Busy Coder's Guide to Android Development 5.5.
Tampa Bay Android Developers Group

Using MediaPlayer and VideoView to Show Video Scott A. Thisse January 22, 2014

Using VideoView and MediaPlayer to Show Video

We'll be looking at two ways to display video content on Android: ●

VideoView widget



MediaPlayer class

January 22, 2014

Tampa Bay Android Developers Group

2 of 17

VideoView Widget



Simplest method to use



Wrapper around MediaPlayer class



Just include in a layout and tell it what/when to play

January 22, 2014

Tampa Bay Android Developers Group

3 of 17

VideoView Example /res/layout/main.xml:

January 22, 2014

Tampa Bay Android Developers Group

4 of 17

VideoView Example, cont /src/org.tbadg/VideoViewActivity.java: package org.tbadg; import android.app.Activity; import android.os.Bundle; import android.widget.VideoView; public class VideoViewActivity extends Activity { private VideoView videoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); videoView = (VideoView) findViewById(R.id.video);

}

}

videoView.setVideoPath("/mnt/sdcard/Movies/bbb.mp4"); videoView.start();

January 22, 2014

Tampa Bay Android Developers Group

5 of 17

Screenshot of VideoView

January 22, 2014

Tampa Bay Android Developers Group

6 of 17

VideoView Playback Controls



Probably want to have playback controls



Add and cross-link a MediaController object



Playback controls will appear when the VideoView widget is touched

January 22, 2014

Tampa Bay Android Developers Group

7 of 17

VideoView with Controls Example

Add the following, along with the required import, to VideoViewActivity.java before the call to start playing the video: // Create and cross-link a controller overlay: MediaController mc; mc = new MediaController(this); mc.setMediaPlayer(videoView); videoView.setMediaController(mc);

January 22, 2014

Tampa Bay Android Developers Group

8 of 17

Screenshot of VideoView with Controls

January 22, 2014

Tampa Bay Android Developers Group

9 of 17

VideoView Widget







Remote videos can be played, but the file format requirements are more stringent See the Android developer reference for a few additional settings and methods More complicated uses may require the use of the more capable MediaPlayer widget

January 22, 2014

Tampa Bay Android Developers Group

10 of 17

MediaPlayer Class

Playing video with the MediaPlayer widget is more complicated: ●

Requires strict adherence to a state machine



Need to supply a Surface object

January 22, 2014

Tampa Bay Android Developers Group

11 of 17

MediaPlayer Class State Machine

January 22, 2014

Tampa Bay Android Developers Group

12 of 17

MediaPlayer

It's not as bad as it looks: ● Just have to always know which state you're currently in ●

This is fairly easy if you always utilize callbacks to handle the transitions

January 22, 2014

Tampa Bay Android Developers Group

13 of 17

MediaPlayer Demo

Let's walk through a demo utilizing the MediaPlayer widget...

January 22, 2014

Tampa Bay Android Developers Group

14 of 17

Screenshot of MediaPlayer Demo

January 22, 2014

Tampa Bay Android Developers Group

15 of 17

References Big Buck Bunny (2008). Retrieved January 22, 2014, from https://archive.org/download/BigBuckBunny_328/BigBuckBunny_512kb.mp4 Komatineni, S. et al. (2012). Pro Android 4. New York, New York: Apress MediaPlayer (2014). Retrieved January 22, 2014, from http://developer.android.com/reference/android/media/MediaPlayer.html Murphy, M. (2014). The Busy Coder's Guide to Android Development 5.5. Retrieved January 22, 2014 from http://commonsware.com VideoView (2014). Retrieved January 22, 2014, from http://developer.android.com/reference/android/widget/VideoView.html

January 22, 2014

Tampa Bay Android Developers Group

16 of 17

Q&A

Questions?

January 22, 2014

Tampa Bay Android Developers Group

17 of 17