First, the Android system comes with Videoview control
1. Create the steps:
(1) Bring your own video files into the Res/raw folder;
(2) Declare the initialization of the Videoview control;
(3) Create the video file URI path, Uri call Parse (), pass in ("android.resource://" +getpackagename () + "/" +r.raw.video) The current string gets the resource path;
(4) To set the playback path for the video control, the Videoview object calls Setvideouri (), passing in the URI object;
(5) Start playback, Videoview object call Start ();
2. Override the control Onmeasure (), set the custom width height of the videoview, Measurespec object Call GetSize () incoming Widthmeasurespec constants and Heighmeasurespec constants to get the screen width and height, After calling Setmeasureddimension (), pass in width and height to set the width and height of the control;
3. Videoview Control Method:
Start (): Starts the video from its current location;
Pause (): Pauses the video;
Isplay (): Determines whether the current video is playing;
Getduration (): Gets the total duration of the currently playing video (in milliseconds);
GetCurrentPosition (): Gets the current video playback duration (in milliseconds);
4. Videoview Monitoring events:
Setonpreparedlistener (): Video ready to listen, incoming implement Mediaplayer.onpreparedlistener interface object, rewrite onprepared (), video ready to play after calling this method;
Setoncompletionlistener (): Video playback complete listening, passing in the implementation of the Mediaplayer.oncompletionlistener interface object, rewrite oncompleted (), after the video playback is complete, call this method;
Setonerrorlistener (): Video playback error listener, incoming object implementing Mediaplayer.onerrorlistener interface, override OnError (), call this method in case of video playback error;
Setontouchlistener (): Touch listen events, pass in an object that implements the View.ontouchlistener interface, override Ontouch (), call this method after the touch control;
Note: If you return false after this method, Event.getaction () can only hear Motionevent.action_down, cannot hear the Motionevent.action_up event, and only returns true. Touch events can be distributed downward to monitor MOTIONEVENT.ACTION_UP events;
5. Videoview video playback control effect with Seekbar:
(1) Initialize the Seekbar and Videoview controls;
(2) Create a URI object using a local path, the URI class calls the static method parse (), passes in the resource path string constant, and the video save path is under the Res/raw folder, the string constant value is: "android.resource://" + Getpackagename () + "/" + r.raw.video_test;
(3) The URI path of the associated Videoview, the Videoview object calls Setvideouri (), and the URI object is passed in;
(4) Start Videoview playback video, Videoview call Start ();
(5) To control the play pause button in the layout to set the Click Listen event, pass in the implementation of the Onclicklistener interface object, override the OnClick (), in this method to determine whether the Videoview control video playback, call IsPlaying (), if it is playing, Videoview call Pause () pause, otherwise videoview call Start (), then get the video current playback progress and total time, put in handler object message queue delay 1000ms send;
(6) Set a value for the Videoview control video playback listener event, call Setonpreparedlistener (), in the overridden onprepared () to get the current playback progress and the total length of time, put in Message Queuing delay 1000ms sent;
(7) Set the value of the Seekbar control progress bar to pull the listener event, call Setonseekbarchangelistener (), pass in the object that implements the Seekbar.onseekbarchangelistener () interface, Override Onprogresschanged () to set the progress for Videoview when Fromuser is true in this method, call the Seekto () to the user to drag the progress, then clear the message, then resend the total time and the progress message;
(8) Set the touch event listener for the Videoview control, call Setontouchlistener (), pass in the object that implements the Ontouchlistener interface, override Ontouch (), in the method if the event is pressed, display the bottom navigation, At the same time, send message notification after 3000ms, hide bottom navigation;
(9) In the message processing event, the message type is judged first, if it is the message that controls the progress bar, the progress bar is reset, the current playback time and the total duration of the Videoview are sent again, and if the message type of the control bar is hidden, the control bar calls Setvisibility (), Incoming View.gone;
Second, Android system bottom Surfaceview control
1. Surfaceview is used in conjunction with MediaPlayer in place of the videoview reason: Videoview comes with MediaPlayer, which makes the system memory too large after being used multiple times in the item in the ListView;
2. Steps to use:
(1) Create the Surfaceview control in the layout file, declare and initialize the control in the activity;
(2) Declaring and instantiating MediaPlayer objects;
(3) Gets the Surfaceholder object, which is used to change the pixel point of Surfaceview, Surfaceview object calls Getholder ();
(4) The Surfaceholder object calls Addcallback (), passing in an object that implements the Surfaceholder.callback interface, overriding three methods:
Method 1:surfacecreate (): This method is called when Surfaceview is created, and video playback is turned on in this method;
Method 2:surfacechanged (): Surfaceview Call this method when the pixel point changes;
Method 3:surfacedestroy (): Surfaceview calls this method after it is destroyed;
(5) Turn on the video playback steps:
(5-1) Reset the Mediaplayer,mediaplayer object before the video playback call reset ();
(5-2) Get the video address, create the URI object, call the static method parse () in the URI class, and pass in the resource path, such as "android:resource://" +getpackagename () + "/" +r.raw.test ";
(5-3) Change the video player settings Surfaceholder,mediaplayer object call Setdisplay (), incoming Surfaceholder object;
(5-4) associated video playback address, MediaPlayer object call Setdatasource () incoming context object and URI object;
(5-5) Open the asynchronous preparation video, MediaPlayer object Call Prepareasync () can;
(6) For MediaPlayer setup ready to complete the listener event, MediaPlayer object calls Setonpreparedlistener (), Pass in an object that implements the Mediaplayer.onpreparedlistener interface, override Onprepared (), and Invoke Start () in the incoming parameter MediaPlayer object within the method;
Android Advanced _ Video playback controls