使用
下载地址:gethub网址:https://github.com/linsea/UniversalVideoView
一, 下载之后导入小项目:会出现报错
- 找到universalvideoview的build.gradle将
android中修改成 buildToolsVersion '25.0.0'
defaultConfig中修改成 minSdkVersion 14
dependencies中的 compile 'com.android.support:appcompat-v7:23.3.0' - 还要把universalvideoviewsample的版本也要更改
buildToolsVersion '25.0.0'
minSdkVersion 14
compile 'com.android.support:appcompat-v7:23.3.0'
就行了
二, 添加依赖
dependencies {
compile 'com.linsea:universalvideoview:1.1.0@aar'
}
好我们先去清单中注册网咯权限
- 权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- 然后将Activity添加旋转屏
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|screenSize">
>
MainActivity代码
package com.universalvideoviewsample;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.universalvideoview.UniversalMediaController;
import com.universalvideoview.UniversalVideoView;
public class MainActivity extends AppCompatActivity implements UniversalVideoView.VideoViewCallback{
private static final String TAG = "MainActivity";
private static final String SEEK_POSITION_KEY = "SEEK_POSITION_KEY";
private static final String VIDEO_URL = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
UniversalVideoView mVideoView;
UniversalMediaController mMediaController;
View mBottomLayout;
View mVideoLayout;
TextView mStart;
private int mSeekPosition;
private int cachedHeight;
private boolean isFullscreen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVideoLayout = findViewById(R.id.video_layout);
mBottomLayout = findViewById(R.id.bottom_layout);
mVideoView = (UniversalVideoView) findViewById(R.id.videoView);
mMediaController = (UniversalMediaController) findViewById(R.id.media_controller);
mVideoView.setMediaController(mMediaController);
setVideoAreaSize();//设置屏幕大小和播放地址
mVideoView.setVideoViewCallback(this);//设置屏幕状态和播放状态的监听
mStart = (TextView) findViewById(R.id.start);
mStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mSeekPosition > 0) {//判断如果播放的进度大于零我将设置进度
mVideoView.seekTo(mSeekPosition);//设置进度
}
mVideoView.start();//播放
mMediaController.setTitle("Big Buck Bunny");//设置标题
}
});
//设置播放完成监听
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.d(TAG, "onCompletion ");
}
});
}
// 暂停
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause ");
if (mVideoView != null && mVideoView.isPlaying()) {
mSeekPosition = mVideoView.getCurrentPosition();
Log.d(TAG, "onPause mSeekPosition=" + mSeekPosition);
mVideoView.pause();
}
}
/**
* 置视频区域大小
*/
private void setVideoAreaSize() {
mVideoLayout.post(new Runnable() {
@Override
public void run() {
int width = mVideoLayout.getWidth();
cachedHeight = (int) (width * 405f / 720f);
// cachedHeight = (int) (width * 3f / 4f);
// cachedHeight = (int) (width * 9f / 16f);
ViewGroup.LayoutParams videoLayoutParams = mVideoLayout.getLayoutParams();
videoLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
videoLayoutParams.height = cachedHeight;
mVideoLayout.setLayoutParams(videoLayoutParams);//设置视频播放视图的大小
mVideoView.setVideoPath(VIDEO_URL);//设置视频播放地址
mVideoView.requestFocus();//获取焦点
}
});
}
//当保存情况状态
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.d(TAG, "onSaveInstanceState Position=" + mVideoView.getCurrentPosition());
outState.putInt(SEEK_POSITION_KEY, mSeekPosition);
}
//当恢复情况状态
@Override
protected void onRestoreInstanceState(Bundle outState) {
super.onRestoreInstanceState(outState);
mSeekPosition = outState.getInt(SEEK_POSITION_KEY);
Log.d(TAG, "onRestoreInstanceState Position=" + mSeekPosition);
}
//全屏和默认的切换
@Override
public void 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);
mBottomLayout.setVisibility(View.GONE);
} else {
ViewGroup.LayoutParams layoutParams = mVideoLayout.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.height = this.cachedHeight;
mVideoLayout.setLayoutParams(layoutParams);
mBottomLayout.setVisibility(View.VISIBLE);
}
switchTitleBar(!isFullscreen);
}
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.d(TAG, "onPause UniversalVideoView callback");
}
@Override
public void onStart(MediaPlayer mediaPlayer) { // 视频开始播放或恢复播放
Log.d(TAG, "onStart UniversalVideoView callback");
}
@Override
public void onBufferingStart(MediaPlayer mediaPlayer) {// 视频开始缓冲
Log.d(TAG, "onBufferingStart UniversalVideoView callback");
}
@Override
public void onBufferingEnd(MediaPlayer mediaPlayer) {// 视频结束缓冲
Log.d(TAG, "onBufferingEnd UniversalVideoView callback");
}
@Override
public void onBackPressed() {
if (this.isFullscreen) {
mVideoView.setFullscreen(false);//不设置全屏
} else {
super.onBackPressed();
}
}
}
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<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:layout_gravity="center"
app:uvv_autoRotation="true"
app:uvv_fitXY="false" />
<com.universalvideoview.UniversalMediaController
android:id="@+id/media_controller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:uvv_scalable="true" />
</FrameLayout>
<LinearLayout
android:id="@+id/bottom_layout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/start"
android:layout_margin="5dp"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@color/uvv_green"
android:gravity="center"
android:text="start" />
<TextView
android:id="@+id/introduction"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="this is video introduciton ......"
android:background="@color/uvv_gray" />
</LinearLayout>
</LinearLayout>
主要布局里写注意
在布局文件中加入自定义View,注意要使UniversalVideoView和UniversalMediaController位于同一个父Layout中, 这样控制条才会浮在视频之上.
<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:layout_gravity="center"
app:uvv_autoRotation="true"
app:uvv_fitXY="false" />
<com.universalvideoview.UniversalMediaController
android:id="@+id/media_controller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:uvv_scalable="true" />
</FrameLayout>