Android video playback (1)

Source: Internet
Author: User
Package cn. c; import java. io. file; import android. app. activity; import android. media. mediaPlayer; import android. media. mediaPlayer. onCompletionListener; import android. media. mediaPlayer. onErrorListener; import android. media. mediaPlayer. onInfoListener; import android. media. mediaPlayer. onPreparedListener; import android. media. mediaPlayer. onSeekCompleteListener; import android. media. mediaPlayer. onVideoSizeChan GedListener; import android. OS. bundle; import android. OS. environment; import android. view. display; import android. view. motionEvent; import android. view. surfaceHolder; import android. view. surfaceView; import android. widget. linearLayout; import android. widget. mediaController; import android. widget. mediaController. mediaPlayerControl;/*** references: http://www.embedu.org/Column/Column503.htm * use MidiaPlayer to play the approximate stream of the video Cheng: * Create A Player Object --> set the source of the video to be played --> * prepare for decoding the video --> start playing --> (pause/restart) the process for stopping playback * defines related methods in MediaPlayer. * after these methods are called, The MediaPlayer can enter different states. * These methods and statuses are well understood. You can refer to them for details. * In addition, after you set the video data source for the video to be played, you need to decode the video (call the prepare () method). * This is a time-consuming operation, to avoid application blocking, * You can use its prepareAsync () method with the OnPreparedListener listener for asynchronous operations. * Note: Only after preparation for MediaPlayer is complete can you start playing the video. * MediaPlayer itself cannot display the video content. It can only control video playback. * if you want to use MediaPlayer to play the video, surfaceVie is also required. W to display the video * and play the video data decoded in the MediaPlayer in SurfaceView. You need to call setDisplay () * in the MediaPlayer to accept a SurfaceHolder object, we can use the SurfaceHolder object corresponding to * SurfaceView for video playback as the parameter *, so that MediaPlayer and SurfaceView are associated. ** MediaPlayer: video playback step * 1. Set the video to be played by the MediaPlayer * mMediaPlayer. setDataSource (path); * 2 set the video output interface of the MediaPlayer * mMediaPlayer. setDisplay (mSurfaceHolder); * 3 MediaPlayer decoding * mMediaPlayer. prepareAsync () combined with * public void OnPrepared (MediaPlayer mediaPlayer) * 4 MediaPlayer starts playing * mediaPlayer. start (); ***/public class MainActivity extends Activity implements OnCompletionListener, OnErrorListener, OnInfoListener, OnPreparedListener, OnSeekCompleteListener, OnVideoSizeChangedListener, SurfaceHolder. callback, MediaPlayerControl {private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; private MediaController m MediaController; private Display mDisplay; private MediaPlayer mMediaPlayer; int videoWidth = 0; int videoHeight = 0; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mDisplay = getWindowManager (). getdefadisplay display (); mSurfaceView = (SurfaceView) findViewById (R. id. surfaceView); mSurfaceHolder = mSurfaceView. getHolder (); // SurfaceH Older is used to manage SurfaceView objects. // how is it managed? Use mSurfaceHolder // to add a callback, that is, addCallback. // This class implements SurfaceHolder. callback interface, so addCallback (this) // In SurfaceHolder. there are three methods in the Callback interface to manage the // SurfaceView object: // surfaceCreated () // surfaceChanged () // surfaceDestroyed () // Overview: surfaceHolder is mainly used to monitor the underlying situation of mSurfaceHolder. addCallback (this); mSurfaceHolder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS); mMediaPlayer = new MediaPlayer (); mMediaPlayer. setOnCompletion Listener (this); mMediaPlayer. setOnErrorListener (this); mMediaPlayer. setOnInfoListener (this); mMediaPlayer. setOnPreparedListener (this); mMediaPlayer. setOnSeekCompleteListener (this); mMediaPlayer. setOnVideoSizeChangedListener (this); String path = Environment. getExternalStorageDirectory (). getPath () + File. separator + "Test_Movie.m4v"; try {// set the video mMediaPlayer to be played by the MediaPlayer. setDataSource (path);} catch (E Xception e) {e. printStackTrace (); finish ();} mMediaController = new MediaController (MainActivity. this) ;}@ Override public boolean onTouchEvent (MotionEvent event) {if (mMediaController. isShowing () {mMediaController. hide ();} else {mMediaController. show ();} return false;} // from SurfaceHolder. callback interface // this method is called when SurfaceView is created. public void surfaceCreated (SurfaceHolder holder) {// sets the video output interface of the MediaPlayer/ /This method is not called when only the audio is displayed but not the video. setDisplay (mSurfaceHolder); try {// prepare for playing and call mMediaPlayer. prepareAsync (); // the overwritten // public void onPrepared (MediaPlayer mp) will be executed. // Note: // you can also call prepare () decoding, but for a synchronous operation mMediaPlayer. prepareAsync ();} catch (Exception e) {e. printStackTrace (); finish () ;}// from SurfaceHolder. callback interface // when the width, height, or other parameters of SurfaceView change, this method is called public void surfaceChanged (SurfaceHolder holder, int format, int Width, int height) {}// from SurfaceHolder. callback interface // when SurfaceView is destroyed, this method is called public void surfaceDestroyed (SurfaceHolder holder) {}// from MediaPlayer. onVideoSizeChangedListener API // call this method when the video width or height changes. public void onVideoSizeChanged (MediaPlayer mp, int width, int height) {}// from MediaPlayer. onSeekCompleteListener interface public void onSeekComplete (MediaPlayer mp) {}// from MediaPlayer. onPreparedListener interface // once this method is called, M The ediaPlayer enters the "ready" // status, ready to start playing. // This can be used to dynamically set the SurfaceView width and height !!! Public void onPrepared (MediaPlayer mediaPlayer) {videoWidth = mediaPlayer. getVideoWidth (); videoHeight = mediaPlayer. getVideoHeight (); if (videoWidth> mDisplay. getWidth () | videoHeight> mDisplay. getHeight () {float heightRatio = (float) videoHeight/(float) mDisplay. getHeight (); float widthRatio = (float) videoWidth/(float) mDisplay. getWidth (); if (heightRatio> 1 | widthRatio> 1) {if (heightRatio> widthRatio) {videoHeight = (int) Math. ceil (float) videoHeight/(float) heightRatio); videoWidth = (int) Math. ceil (float) videoWidth/(float) heightRatio);} else {videoHeight = (int) Math. ceil (float) videoHeight/(float) widthRatio); videoWidth = (int) Math. ceil (float) videoWidth/(float) widthRatio) ;}}// set the width and height of SurfaceView mSurfaceView. setLayoutParams (new LinearLayout. layoutParams (videoWidth, videoHeight); // you can set and use mMediaController only after the MediaPlayer is ready. setMediaPlayer (this); mMediaController. setAnchorView (findViewById (R. id. mainView); mMediaController. setEnabled (true); mMediaController. show (); // MediaPlayer starts playing mediaPlayer. start ();} // from MediaPlayer. onInfoListener interface // This method will be called when specific information about the playing media is displayed or a warning is required. // For example, start buffering, buffer end, and download speed change (this row is awaiting verification)) // Summary: all these Info items are MediaPlayer. public boolean onInfo (MediaPlayer mp, int what, int extra) {if (what = MediaPlayer. MEDIA_INFO_BAD_INTERLEAVING) {// This prompt is displayed when audio and video data are incorrectly staggered. in a // correctly staggered media file, audio and video samples are arranged in sequence so that the video can be played effectively and stably.} if (what = MediaPlayer. MEDIA_INFO_NOT_SEEKABLE) {// This prompt is displayed when the media cannot be correctly located. // This means it may be an online stream} if (what = MediaPlayer. MEDIA_INFO_VIDEO_TRACK_LAGGING) {// This prompt is displayed when the device cannot play the video. // For example, the video is too complex or the bit rate is too high.} if (what = MediaPlayer. MEDIA_INFO_METADATA_UPDATE) {// This prompt will appear when the new metadata is available} if (what = MediaPlayer. MEDIA_INFO_UNKNOWN) {// other unknown message} return false;} // from MediaPlayer. onErrorListener interface // This method will be called when a MediaPlayer error occurs // There are only the following three errors. // conclusion: all these errors are MediaPlayer. MEDIA_ERROR. public boolean onError (MediaPlayer mp, int what, int extra) {if (what = MediaPlayer. MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) {System. out. println ("first error");} if (what = MediaPlayer. MEDIA_ERROR_SERVER_DIED) {System. out. println ("second error");} if (what = MediaPlayer. MEDIA_ERROR_UNKNOWN) {System. out. println ("third error");} return false;} // from MediaPlayer. onCompletionListener interface // this method is called when the MediaPlayer finishes playing the file. // you can perform other operations at this time, such as playing the next video public void onCompletion (MediaPlayer mp) {finish () ;}// the following methods are all from the MediaPlayerControl interface public void start () {mMediaPlayer. start ();} public void pause () {if (mMediaPlayer. isPlaying () {mMediaPlayer. pause () ;}} public int getDuration () {return mMediaPlayer. getDuration ();} public int getCurrentPosition () {return mMediaPlayer. getCurrentPosition ();} public void seekTo (int pos) {mMediaPlayer. seekTo (pos);} public boolean isPlaying () {return mMediaPlayer. isPlaying ();} public int getBufferPercentage () {// TODO Auto-generated method stubreturn 0;} public boolean canPause () {return true;} public boolean canSeekBackward () {return true;} public boolean canSeekForward () {return true ;}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.