Android advanced_ Video playback control

1、 The Android system comes with its own videoview control

1. Creation steps:

(1) Put your own video files into the RES / raw folder;

(2) Declare to initialize the videoview control;

(4) Set the playback path for the video control, and the videoview object calls setvideouri() to pass in the URI object;

(5) When playback starts, the videoview object calls start();

2. Override the control onmeasure() to set the custom width and height of videoview; The MeasureSpec object calls getSize () to import the widthMeasureSpec constants and heighMeasureSpec constants to get the screen width and height, then setMeasuredDimension (), the width and height of the incoming widths and height settings.

3. Videoview control method:

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

Pause(): pause the video;

Isplay(): judge whether the current video is playing;

Getduration(): get the total duration of the currently playing video (unit: ms);

Getcurrentposition(): obtain the current video playback duration (unit: ms);

4. Videoview listening events:

SetOnPreparedListener (): the video prepares to monitor, transfers to the MediaPlayer.OnPreparedListener realization object, rewrites onPrepared (), after the video prepares to broadcast, calls this method;

SetOnCompletionListener (): video playback completes listener, incoming to MediaPlayer.OnCompletionListener interface object, rewrite onCompleted (), after video playback is completed, call this method.

Setonerrorlistener(): listen for video playback errors, pass in the object implementing mediaplayer.onerrorlistener interface, override onerror(), and call this method in case of video playback errors;

SetOnTouchListener (): touches the monitor event, passes to the object which realizes the View.OnTouchListener interface, rewrites onTouch (), after touches the control to call this method;

Note: if false is returned after this method, event. Getaction () can only listen to motionevent. Action_ Down, unable to listen to motionevent.action_ Up event; Only by returning true, the touch event can be distributed downward, and motionevent.action can be monitored_ Up event;

5. Video playback control effect of videoview and seekbar:

(1) Initialize seekbar and videoview controls;

(3) Associated with the URI path of videoview, the videoview object calls setvideouri() to pass in the URI object;

(4) Start videoview to play video, and videoview calls start();

(5) Set the click listening event for the play pause button in the control layout, pass in the object implementing the onclicklistener interface, override onclick(), judge whether the video of the videoview control is playing in this method, call isplaying(), and call pause() if it is playing; Otherwise, videoview calls start() to start, then obtains the current playback progress and total duration of the video, puts it into the message queue of the handler object, and delays sending for 1000ms;

(6) Set a value for the video playback listening event for the videoview control, call setonpreparedlistener(), get the current playback progress and total duration in the rewritten onprepared(), put it into the message queue and delay sending for 1000ms;

(7) Set the value for the seekbar control progress bar to pull the listening event, call setonseekbarchangelistener(), pass in the object implementing the seekbar. Onseekbarchangelistener() interface, override onprogresschanged(), set the progress for videoview when judging that fromuser is true in this method, and call seekto() to pass in the user drag progress; After that, clear the message first, and then resend the total duration and playback progress message;

(8) Set touch event listening for the videoview control, call setontouchlistener(), pass in the object implementing the ontouchlistener interface, and override ontouch(). If the event is pressed in this method, the bottom navigation will be displayed, and a message notification will be sent after 3000ms to hide the bottom navigation;

(9) In the message processing event, first judge the message type. If it is a message controlling the progress bar, reset the progress bar, and then send the current playback time and total duration of videoview again; If it is a message type that hides the control bar, the control bar calls setvisibility() and passes in view. Gone;

2、 Underlying surfaceview control of Android system

1. The reason why surfaceview is combined with mediaplayer to replace videoview: mediaplayer comes with videoview, which will cause excessive system memory after being used in items in listview for many times;

2. Use 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) Get the surfaceholder object, which is used to change the pixels of the surfaceview. The surfaceview object calls getholder();

(4) The surfaceholder object calls addcallback(), passes in the object implementing the surfaceholder.callback interface, and overrides the three methods:

Method 1: surfacecreate(): call this method when the surfaceview is created, and start video playback in this method;

Method 2: surfacechanged(): surfaceview calls this method when the pixel changes;

Method 3:surfaceDestroy ():SurfaceView is called after destruction.

(5) To start video playback:

(5-1) reset mediaplayer before video playback, and the mediaplayer object calls reset();

Invalid Sign

(5-3) change the video player to set the surfaceholder, and the mediaplayer object calls setdisplay() to pass in the surfaceholder object;

(5-4) associate the video playback address, and the mediaplayer object calls setdatasource() to pass in the context object and URI object;

(5-5) start asynchronous video preparation, and the mediaplayer object can call prepareasync();

(6) Set the preparation completion listening event for mediaplayer. The mediaplayer object calls setonpreparedlistener(), passes in the object implementing the mediaplayer.onpreparedlistener interface, overrides onprepared(), and calls start() for the passed in formal parameter mediaplayer object in this method;

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>