You can play a video in either of the following ways:
Video playback using VideoView (convenient and recommended)
Play a video using MediaPlayer and SurfaceView (earlier method)
Method 1:
To use VideoView to play a video, follow these steps:
Define the VideoView component in the interface layout file or create a VideoView component in the program
Call the following two methods of VideoView to load the specified video:
SetVideoPath (String paht): loads the video represented by the path file.
SetVideoURI: loads the video corresponding to the URI
Call the start (), stop (), and pause () Methods of VideoVIew to control video playback.
MediaContraller class
In combination with VideoView, there is also a MediaContraller class, which provides a friendly graphic control interface, through which you can control video playback, fast forward key, and seek key, back key and playback progress
Items are provided by this class
Sample Code:
Xml layout:
Activity:
Import java. io. file; import android. app. activity; import android. graphics. pixelFormat; import android. OS. bundle; import android. widget. mediaController; import android. widget. videoView; public class VedioViewTest extends Activity {VideoView videoView; MediaController mController; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); getWindow (). setFormat (PixelFormat. TRANSLUCENT); setContentView (R. layout. main); // obtain the VideoView component videoView = (VideoView) findViewById (R. id. video); // create the MediaController object mController = new MediaController (this); File video = new File ("/mnt/sdcard/movie.mp4"); if (video. exists () {videoView. setVideoPath (video. getAbsolutePath (); // ① // sets the videoView to be associated with the mController. setMediaController (mController); // ② // you can set up an association between mController and videoView. setMediaPlayer (videoView); // ③ // obtain the VideoView focus. requestFocus ();}}}
Method 2:
To use MediaPlayer to play a video, follow these steps:
Create a MediaPlayer object and load the specified video file.
Define the SurfaceView component in the interface layout file, or create a SurfaceView component in the program, and add a Callback listener for the SurfaceView of SurfaceView.
Call setPisplay (SurfaceHolder sh) of the MediaPlayer object to output the video image to the specified SurfaceView component.
Call the start (), stop (), and pause () Methods of the MediaPlayer object to control video playback.
Sample Code:
Xml layout:
Activity:
Import java. io. IOException; import android. app. activity; import android. media. audioManager; import android. media. mediaPlayer; import android. OS. bundle; import android. util. displayMetrics; import android. view. surfaceHolder; import android. view. surfaceView; import android. view. view; import android. view. view. onClickListener; import android. view. windowManager; import android. widget. relativeLayout. layoutParams; import android. widget. imageButton; public class SurfaceViewPlayVideo extends Activityimplements OnClickListener {SurfaceView surfaceView; ImageButton play, pause, stop; MediaPlayer mPlayer; // records the int position of the current video; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain the three buttons play = (ImageButton) findViewById (R. id. play); pause = (ImageButton) findViewById (R. id. pause); stop = (ImageButton) findViewById (R. id. stop); // click the event to bind the event listener play. setOnClickListener (this); pause. setOnClickListener (this); stop. setOnClickListener (this); // create MediaPlayermPlayer = new MediaPlayer (); surfaceView = (SurfaceView) this. findViewById (R. id. surfaceView); // set the surfaceView to be enabled during playback. getHolder (). setKeepScreenOn (true); surfaceView. getHolder (). addCallback (new SurfaceListener () ;}@ Overridepublic void onClick (View source) {try {switch (source. getId () {// the play button is clicked case R. id. play: play (); break; // the pause button is clicked case R. id. pause: if (mPlayer. isPlaying () {mPlayer. pause ();} else {mPlayer. start ();} break; // the stop button is clicked by case R. id. stop: if (mPlayer. isPlaying () mPlayer. stop (); break;} catch (Exception e) {e. printStackTrace () ;}} private void play () throws IOException {mPlayer. reset (); mPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC); // sets the video mPlayer to be played. setDataSource ("/mnt/sdcard/movie.3gp"); // output the video image to SurfaceViewmPlayer. setDisplay (surfaceView. getHolder (); // ① mPlayer. prepare (); // obtain the window manager WindowManager wManager = getWindowManager (); DisplayMetrics metrics = new DisplayMetrics (); // obtain the screen size wManager. getdefadisplay display (). getMetrics (metrics); // sets the video aspect ratio to zoom to the full screen surfaceView. setLayoutParams (new LayoutParams (metrics. widthPixels, mPlayer. getVideoHeight () * metrics. widthPixels/mPlayer. getVideoWidth (); mPlayer. start ();} private class SurfaceListener implements SurfaceHolder. callback {@ Overridepublic void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {}@ Overridepublic void surfaceCreated (SurfaceHolder holder) {if (position> 0) {try {// start playing play (); // and play the mPlayer directly from the specified position. seekTo (position); position = 0;} catch (Exception e) {e. printStackTrace () ;}}@ Overridepublic void surfaceDestroyed (SurfaceHolder holder) {}} // when other activities are enabled, pause playing @ Overrideprotected void onPause () {if (mPlayer. isPlaying () {// Save the current position = mPlayer. getCurrentPosition (); mPlayer. stop ();} super. onPause () ;}@ Overrideprotected void onDestroy () {// stop playing if (mPlayer. isPlaying () mPlayer. stop (); // release the resource mPlayer. release (); super. onDestroy ();}}