The easiest way to play a video on Android is to use the built-in Videoview in the SDK, and then add Mediacontroller to control video playback pauses, but there's a drawback that you can't customize your own control UI, so here's a custom playback control. It can set a variety of custom properties (such as loading style, error video, etc.), and it is easy to switch between full screen and non-full screen, in addition to support Android V2.3 and above system.
Project Address: Https://github.com/linsea/UniversalVideoView
How to use
Please refer to the sample in the project for the complete use method.
1. Add the following dependency package on the Build.gradle file.
dependencies { compile ' com.linsea:universalvideoview:[email protected] '}
2. Add a custom view to the layout file, and note that you want the Universalvideoview and Universalmediacontroller to be in the same parent layout so that the control bar floats above the video.
<framelayout android:id= "@+id/video_layout" android:layout_width= "Fill_parent" android:layout_height= "200DP" android:background= "@android: Color/black" > <com.universalvideoview.universalvideoview android:id= "@+id/videoview" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" Android:la yout_gravity= "Center" app:uvv_fitxy= "false"/> <com.universalvideoview.universalme Diacontroller android:id= "@+id/media_controller" android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" app:uvv_scalable= "true"/> </fr Amelayout>
3. Set the listener for the related event in the OnCreate method.
View mbottomlayout; View mvideolayout; Universalvideoview Mvideoview; Universalmediacontroller Mmediacontroller;mvideoview = (universalvideoview) Findviewbyid (R.id.videoView); Mmediacontroller = (Universalmediacontroller) Findviewbyid (R.id.media_controller); Mvideoview.setmediacontroller ( Mmediacontroller); Mvideoview.setvideoviewcallback (new Universalvideoview.videoviewcallback () {@Override public voi D Onscalechange (Boolean isfullscreen) {this.isfullscreen = IsFullScreen; if (isfullscreen) {Viewgroup.layoutparams layoutparams = Mvideolayout.getlayoutparams (); Layoutparams.width = ViewGroup.LayoutParams.MATCH_PARENT; Layoutparams.height = ViewGroup.LayoutParams.MATCH_PARENT; Mvideolayout.setlayoutparams (Layoutparams); When setting the full screen, unrelated View disappears to allow maximum position mbottomlayout.setvisibility (View.gone) for the video control and controller controls; } else {Viewgroup.layoutparams layoutparams = Mvideolayout.getlayoutparams (); LaYoutparams.width = ViewGroup.LayoutParams.MATCH_PARENT; Layoutparams.height = This.cachedheight; Mvideolayout.setlayoutparams (Layoutparams); Mbottomlayout.setvisibility (view.visible); }} @Override public void OnPause (MediaPlayer MediaPlayer) {//Video pause log.d (TAG, "OnPause Universalvideovie W callback "); } @Override public void OnStart (MediaPlayer MediaPlayer) {//video starts playing or resumes playback log.d (TAG, "OnStart Universalvideov Iew callback "); } @Override public void Onbufferingstart (MediaPlayer MediaPlayer) {//Video start buffering log.d (TAG, "Onbufferingstart Uni Versalvideoview callback "); } @Override public void Onbufferingend (MediaPlayer MediaPlayer) {//video end buffer log.d (TAG, "Onbufferingend Univers Alvideoview callback "); }});
Attention:
Universalvideoview does not save the playback status, such as playing to the first few minutes, so you need to apply to save these states and restore.
To ensure that the system restarts the activity when the screen is rotated, you need to add the activity's properties:
Android:configchanges= "Orientation|keyboardhidden|screensize
4 Customizing properties
To ensure the flexibility of the custom UI, the following properties are available:
Uvv_fitxy
The Universalvideoview property, Boolean value, true when setting video scaling with the width and height of the view settings in the X, y direction, which may distort the video. False when zooming maintains the length-to-width ratio of the video, similar to the Videoview in the SDK.
uvv_scalable
The Universalmediacontroller property, Boolean value, displays whether the Zoom button at the bottom right of the control bar is displayed, or False if you do not want to play the full screen.
Define player controls on Android Universalvideoview