Android _ video playback control and android playback control

Source: Internet
Author: User

Android _ video playback control and android playback control

1. The VideoView control is provided in the Android system.

1. Creation steps:

(1) Add the built-in video file to the res/raw folder;

(2) declare and initialize the VideoView control;

(3) create the video file Uri path. The Uri calls parse () and passes in ("android. resource: // "+ getPackageName () +"/"+ R. raw. video) The current string to obtain the resource path;

(4) set the playback path for the video control. The VideoView object calls setVideoURI () to pass in the Uri object;

(5) start playing. The VideoView object calls start ();

2. override the onMeasure () Control and set the custom width and height of VideoView. The MeasureSpec object calls getSize () to pass in the widthMeasureSpec constant and heighMeasureSpec constant to get the screen width and height, and then calls setMeasuredDimension (), specify the width and height of the widget;

3. VideoView control method:

Start (): starts the video from the current position;

Pause (): pause the video;

IsPlay (): determines whether the current video is being played;

GetDuration (): Get the total duration of the current video (unit: milliseconds );

GetCurrentPosition (): gets the current video playback duration (unit: milliseconds );

4. VideoView listening event:

SetOnPreparedListener (): This method is used to prepare a video listener, input an object that implements the MediaPlayer. OnPreparedListener interface, and overwrite onPrepared (). This method is called after the video is ready to be played;

SetOnCompletionListener (): This method is used to listen for video playback. It is used to input an object that implements the MediaPlayer. OnCompletionListener interface, overwrite onCompleted (), and call this method after video playback is complete;

SetOnErrorListener (): Specifies the video playback error listener. It is used to input an object that implements the MediaPlayer. OnErrorListener interface, override onError (), and call this method when a video playback error occurs;

SetOnTouchListener (): Touch the listener event, pass in the object implementing the View. OnTouchListener interface, override onTouch (), and call this method after touching the control;

NOTE: If false is returned after this method, the event. getAction () can only listen to MotionEvent. ACTION_DOWN, unable to listen to MotionEvent. ACTION_UP event; only when true is returned can the touch event be distributed downstream to listen to MotionEvent. ACTION_UP event;

5. video playback control effects of VideoView and Seekbar:

(1) initialize the Seekbar and VideoView controls;

(2) create a Uri object using the local path. The Uri class calls the static method parse () and passes in the resource path string constant. The video storage path is in the res/raw folder, the constant value of the string is: "android. resource: // "+ getPackageName () +"/"+ R. raw. video_test;

(3) associate the Uri path of the VideoView. The VideoView object calls setVideoURI () to pass in the Uri object;

(4) start VideoView to play the video. VideoView calls start ();

(5) set the click listening event for the play pause button in the control layout, input the object implementing the onClickListener interface, overwrite onClick (), and determine whether the video of the VideoView control is played in this method, call isPlaying (). If the video is being played, VideoView calls pause () to pause; otherwise, VideoView calls start () to enable and then obtains the current video playback progress and duration, the message queue of the Handler object is sent with a delay of Ms;

(6) set a video playback listener event for the VideoView control, call setOnPreparedListener (), get the current playback progress and total duration in the rewritten onPrepared (), and put it in the message queue for sending with a delay of Ms;

(7) set a value for the progress bar of the Seekbar control to pull the listening event, call setOnSeekBarChangeListener (), and pass in the SeekBar. onSeekBarChangeListener () interface object, override onProgressChanged (), in this method, when the fromUser is determined to be true, set the progress for VideoView, call seekTo () to input the user's drag progress; then clear the message first, resend the total duration and playback progress message;

(8) set the touch event listener for the VideoView control, call setOnTouchListener (), pass in the object implementing the OnTouchListener interface, and override onTouch (). In this method, if the event is pressed, display the bottom navigation, send a message notification after MS at the same time, hide the bottom navigation;

(9) In A Message Processing Event, first determine the message type. If the message controls the progress bar, reset the progress bar and then re-Send the current playback time and total duration of the VideoView; if the message type of the control bar is hidden, the control bar calls setVisibility () to pass in the View. GONE;

 

Ii. SurfaceView controls at the underlying layer of the Android System

1. SurfaceView and MediaPlayer are used in combination to replace VideoView. The reason is that the MediaPlayer in VideoView is self-contained. If it is used multiple times in the Item in ListView, the system memory will be too large;

2. steps:

(1) create a SurfaceView control in the layout file, declare and initialize the control in the Activity;

(2) declare and instantiate the MediaPlayer object;

(3) obtain the SurfaceHolder object, which is used to change the SurfaceView pixel. The SurfaceView object calls getHolder ();

(4) The SurfaceHolder object calls addCallback (), passes in the object implementing the SurfaceHolder. Callback interface, and overrides three methods:

Method 1: surfaceCreate (): This method is called when SurfaceView is created. Video Playback is enabled in this method;

Method 2: surfaceChanged (): This method is called when the pixel changes;

Method 3: surfaceDestroy (): This method is called after SurfaceView is destroyed;

(5) To enable video playback, follow these steps:

(5-1) reset MediaPlayer before video playback. The MediaPlayer object calls reset ();

(5-2) obtain the video address and create a Uri object. The Uri class calls the static method parse () to pass in the resource path, for example, "" android: resource: // "+ getPackageName () + "/" + R. raw. test ";

(5-3) modify the video player settings SurfaceHolder. The MediaPlayer object calls setDisplay () to pass in the SurfaceHolder object;

(5-4) associate the video playback address. The MediaPlayer object calls setDataSource () to pass in the context object and Uri object;

(5-5) Enable asynchronous video preparation. The MediaPlayer object calls prepareAsync;

(6) configure the listener for the MediaPlayer. The MediaPlayer object calls setOnPreparedListener () to pass in the listener to implement the MediaPlayer. onPreparedListener interface object, override onPrepared (), call start () for the input parameter MediaPlayer object in this method ();

 

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.