在Android上播放视频最简单的方法是使用SDK中内置的VideoView,然后加上MediaController来控制视频播放暂停等,但是这样有一个缺点是无法定制自己的控制UI,所以这里提供一个自定义播放控件,它可以设置多种自定义属性(如loading样式,错误视频等),并且支持根据重力感应在全屏与非全屏之间自动切换,另外支持Android V2.3及以上系统。
UniversalVideoView的下载地址:https://github.com/linsea/UniversalVideoView
演示demo:
public class MainActivity extends AppCompatActivity { private static final String TAG = "print"; private UniversalVideoView mVideoView; private UniversalMediaController mControl; private static final String VIDEO_URL = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"; private FrameLayout videoLayout; private int mSeekPPosition; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { videoLayout = (FrameLayout) findViewById(R.id.video_layout); mVideoView = (UniversalVideoView) findViewById(R.id.videoview); mControl = (UniversalMediaController) findViewById(R.id.media_control); mVideoView.setMediaController(mControl); mVideoView.setVideoPath(VIDEO_URL); mVideoView.requestFocus(); mSeekPPosition = mVideoView.getCurrentPosition();//获取当前播放的进度 mVideoView.setVideoViewCallback(new UniversalVideoView.VideoViewCallback() { @Override public void onScaleChange(boolean isFullscreen) { if(isFullscreen){ ViewGroup.LayoutParams parmas = videoLayout.getLayoutParams(); parmas.height = ViewGroup.LayoutParams.MATCH_PARENT; parmas.width = ViewGroup.LayoutParams.MATCH_PARENT; videoLayout.setLayoutParams(parmas); }else{ ViewGroup.LayoutParams parmas = videoLayout.getLayoutParams(); parmas.height = ViewGroup.LayoutParams.WRAP_CONTENT; parmas.width = ViewGroup.LayoutParams.MATCH_PARENT; videoLayout.setLayoutParams(parmas); } setActionBar(!isFullscreen); } @Override public void onPause(MediaPlayer mediaPlayer) { Log.d(TAG, "onPause: "+"视频暂停播放"); } @Override public void onStart(MediaPlayer mediaPlayer) { Log.d(TAG, "onStart: "+"视频开始播放"); } @Override public void onBufferingStart(MediaPlayer mediaPlayer) { Log.d(TAG, "onBufferingStart: "+"视频开始缓冲"); } @Override public void onBufferingEnd(MediaPlayer mediaPlayer) { Log.d(TAG, "onBufferingEnd: "+"视频缓冲结束"); } }); mVideoView.start(); } //控制ActionBar的显示与隐藏 private void setActionBar(boolean isFullscreen){ ActionBar actionBar = getSupportActionBar(); if(isFullscreen){ actionBar.show(); }else{ actionBar.hide(); } } }XML文件:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.wls.universalvideoview.MainActivity">
android:id="@+id/video_layout"
android:layout_width="match_parent"
android:layout_height="200dp">
android:id="@+id/videoview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
app:uvv_autoRotation="true"
app:uvv_fitXY="false"/>
android:id="@+id/media_control"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:uvv_scalable="true"/>
注意:UniversalVideoView和UniversalVideoView要在同一个父Layout中,这样进度条才会浮现在视频上
其中,为了避免屏幕旋转时重启Activity,在AndroidManifest中设置
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize">
定制属性:
uvv_fitXY
UniversalVideoView的属性,布尔值,true时设置视频缩放时在X,Y方向上铺满View设置的宽度和高度,这样可能使视频变形.false时则缩放时保持视频的长宽比例,与SDK中的VideoView类似.
uvv_autoRotation
UniversalVideoView的属性,布尔值,true时视频会根据重力响应通知客户进行全屏与非全屏之间的切换.
uvv_scalable
UniversalMediaController属性,布尔值,是否显示控制条右下方的缩放按钮,如果不想全屏播放时,可以设置为false不显示.
效果图:竖屏时:
全屏时:
如果,想要显示视频的名称,可在demo中加上一句:
mControl = (UniversalMediaController) findViewById(R.id.media_control); mControl.setTitle("Big Buck Bunny......"); mVideoView.setMediaController(mControl);此时,效果为: