MyVieoView.java
3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.share.mvpsdk.view;
import android.content.Context;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;
/**
* Created by ToaHanDong on 2018/4/10.
*/
public class MyVieoView extends VideoView {
private Context mContext = null;
//最终的视频资源宽度
private int mVideoWidth = 480;
//最终视频资源高度
private int mVideoHeight = 480;
//视频资源原始宽度
private int videoRealW = 1;
//视频资源原始高度
private int videoRealH = 1;
private String url = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
public MyVieoView(Context context) {
super(context);
mContext = context;
}
public MyVieoView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
public MyVieoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
}
public void setVideoPath(String urlPath) {
// if (TextUtils.isEmpty(urlPath)) urlPath = url;
setVideoURI(Uri.parse(urlPath));
//创建视频播放时的控制器,这个控制器可以自定义。此处是默认的实现
setMediaController(null);
//请求焦点
requestFocus();
//设置播放监听
setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// optional need Vitamio 4.0
//设置重放速度
// mediaPlayer.setPlaybackSpeed(1.0f);
}
});
//加载结束后开始播放,这行代码可以控制视频的播放。
start();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = getDefaultSize(0, widthMeasureSpec);
int height = getDefaultSize(0, heightMeasureSpec);
if (height > width) {
//竖屏
if (videoRealH > videoRealW) {
//如果视频资源是竖屏
//占满屏幕
mVideoHeight = height;
mVideoWidth = width;
} else {
//如果视频资源是横屏
//宽度占满,高度保存比例
mVideoWidth = width;
float r = videoRealH / (float) videoRealW;
// mVideoHeight = (int) (mVideoWidth * r);
mVideoHeight=height;
}
} else {
//横屏
if (videoRealH > videoRealW) {
//如果视频资源是竖屏
//宽度占满,高度保存比例
mVideoHeight = height;
float r = videoRealW / (float) videoRealH;
mVideoWidth = (int) (mVideoHeight * r);
} else {
//如果视频资源是横屏
//占满屏幕
mVideoHeight = height;
mVideoWidth = width;
}
}
setMeasuredDimension(mVideoWidth, mVideoHeight);
// if (videoRealH == videoRealW && videoRealH == 1) {
// //没能获取到视频真实的宽高,自适应就可以了,什么也不用做
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// } else {
// setMeasuredDimension(mVideoWidth, mVideoHeight);
// }
}
}