android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="this is video introduction ......" />
step3、在activity中
public class MainActivity extends AppCompatActivity implements UniversalVideoView.VideoViewCallback {
private View videoLayout;
private View bottomLayout;
private UniversalVideoView videoView;
private UniversalMediaController mediaController;
private int seekPosition;//当前进度
private int cachedHeight;//视频区域大小
private boolean isFullscreen;//是否全屏
private static final String VIDEO_URL = "http://vf1.mtime.cn/Video/2017/03/15/mp4/170315222409670447.mp4";
private static final String SEEK_POSITION_KEY = "SEEK_POSITION_KEY";//用于保存seekPosition的key值
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
intiEvent();
}
/**
* 初始化控件
*/
private void initView() {
videoLayout = findViewById(R.id.videoLayout);
bottomLayout = findViewById(R.id.bottomLayout);
videoView = (UniversalVideoView) findViewById(R.id.videoView);
mediaController = (UniversalMediaController) findViewById(R.id.mediaController);
}
private void intiEvent() {
//把视频控制的按钮设置到播放器里
videoView.setMediaController(mediaController);
//设置置视频区域大小
setVideoAreaSize();
//设置屏幕状态和播放状态的监听
videoView.setVideoViewCallback(this);
}
/**
* 设置置视频区域大小和播放地址
*/
private void setVideoAreaSize() {
videoLayout.post(new Runnable() {
@Override
public void run() {
int width = videoLayout.getWidth();
cachedHeight = (int) (width * 405f / 720f);
// cachedHeight = (int) (width * 3f / 4f);
// cachedHeight = (int) (width * 9f / 16f);
ViewGroup.LayoutParams videoLayoutParams = videoLayout.getLayoutParams();
videoLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
videoLayoutParams.height = cachedHeight;
videoLayout.setLayoutParams(videoLayoutParams);
videoView.setVideoPath(VIDEO_URL);
videoView.requestFocus();
}
});
}
/**
* 开始按钮
*
* @param v
*/
public void startClick(View v) {
//设置视频开始
videoView.start();
//设置视频标题
mediaController.setTitle("寻梦环游记");
}
//--------------重写下面三个方面是为了保存seekPosition--------------
@Override
protected void onPause() {
super.onPause();
if (videoView != null && videoView.isPlaying()) {
seekPosition = videoView.getCurrentPosition();
Log.i("info", "onPause mSeekPosition=" + seekPosition);
videoView.pause();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i("info", "onSaveInstanceState Position=" + videoView.getCurrentPosition());
outState.putInt(SEEK_POSITION_KEY, seekPosition);
}
@Override
protected void onRestoreInstanceState(Bundle outState) {
super.onRestoreInstanceState(outState);
seekPosition = outState.getInt(SEEK_POSITION_KEY);
Log.i("info", "onRestoreInstanceState Position=" + seekPosition);
}
//--------------以下方法都是VideoViewCallback接口的实现方法--------------
/**
* 全屏和默认的切换
*
* @param isFullscreen
*/
@Override
public void onScaleChange(boolean isFullscreen) {
this.isFullscreen = isFullscreen;
if (isFullscreen) {
ViewGroup.LayoutParams layoutParams = videoLayout.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
videoLayout.setLayoutParams(layoutParams);
//设置全屏时,无关的View消失,以便为视频控件和控制器控件留出最大化的位置
bottomLayout.setVisibility(View.GONE);
} else {
ViewGroup.LayoutParams layoutParams = videoLayout.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.height = this.cachedHeight;
videoLayout.setLayoutParams(layoutParams);
bottomLayout.setVisibility(View.VISIBLE);
}
switchTitleBar(!isFullscreen);
}
/**
* 横竖屏切换的时候控制ActionBar的状态
*
* @param show
*/
private void switchTitleBar(boolean show) {
android.support.v7.app.ActionBar supportActionBar = getSupportActionBar();
if (supportActionBar != null) {
if (show) {
supportActionBar.show();
} else {
supportActionBar.hide();
}
}
}
@Override
public void onPause(MediaPlayer mediaPlayer) {// 视频暂停
Log.i("info", "onPause");
}
@Override
public void onStart(MediaPlayer mediaPlayer) {// 视频开始播放或恢复播放
Log.i("info", "onStart");
}
@Override
public void onBufferingStart(MediaPlayer mediaPlayer) {// 视频开始缓冲
Log.i("info", "onBufferingStart");
}
@Override
public void onBufferingEnd(MediaPlayer mediaPlayer) {// 视频结束缓冲
Log.i("info", "onBufferingEnd");
}
@Override
public void onBackPressed() {
if (this.isFullscreen) {
videoView.setFullscreen(false);
} else {
super.onBackPressed();
}
}
}
step4、在AndroidManifest.xml添加配置
权限:
activity属性:android:configChanges="orientation|keyboardHidden|screenSize"
属性介绍:
uvv_fitXY:
UniversalVideoView的属性,布尔值,true时设置视频缩放时在X,Y方向上铺满View设置的宽度和高度,这样
能使视频变形.false时则缩放时保持视频的长宽比例,与SDK中的VideoView类似.
uvv_autoRotation:
UniversalVideoView的属性,布尔值,true时视频会根据重力响应通知客户进行全屏与非全屏之间的切换.
uvv_scalable:
UniversalMediaController属性,布尔值,是否显示控制条右下方的缩放按钮,如果不想全屏播放时,可以设
置为false不显示.