我使用安卓系统中的Mediaplayer在线播放视频,并将其显示在视频视图上。视频视图放在一个固定大小的RelativeLayout中。
问题是视频的大小(视频的宽度和高度)不适合相对输出,所以我需要调整视频的大小,使其与视频的精确比例相匹配,但似乎不适用,我在这里的代码如下:
*创建视频视图的代码:
...
MyVideoView mVideoView = new MyVideoView(mContext);
mVideoView.requestFocus();
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
mVideoView.setLayoutParams(p);
return mVideoView;
}*视频开始播放时获取比率的代码
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mWebview.removeView(mProgressWheel);
mediaPlayer.start();
// int w = mediaPlayer.getVideoWidth();
// int h = mediaPlayer.getVideoHeight();
int w = mVideoView.getWidth();
int h = mVideoView.getHeight();
((MyVideoView) mVideoView).setDimention(w, h);
}*我的自定义视频视图类内容不足:
public class MyVideoView extends VideoView{
...
private ElementObject elm;
private int aW;
private int aH;
int additional = 0;
float ratio = 1f;
private void caculateRatio(int w, int h){
if(w > h) ratio = (float)w/h;
else if(h > w) ratio = (float)h/w;
}
public void setDimention(int w, int h){
this.aW = w;
this.aH = h;
setIsPlaying(true);
caculateRatio(w, h);
calculateAdditional(w, h);
}
public void calculateAdditional(int w, int h){
if(w >= elm.frame.getW() && h < elm.frame.getH()) {
additional = elm.frame.getH() - h;
measure(w + (int) (additional * ratio), elm.frame.getH());
}else if(w < elm.frame.getW() && h >= elm.frame.getH()) {
additional = elm.frame.getW() - w;
measure(elm.frame.getW(), h + (int) (additional * ratio));
}else if(w < elm.frame.getW() && h < elm.frame.getH()){
int wR = elm.frame.getW() - w;
int hR = elm.frame.getH() - h;
if(wR > hR){
additional = wR;
measure(elm.frame.getW(), h + (int) (additional * ratio));
}else {
additional = hR;
measure(w + (int) (additional * ratio), elm.frame.getH());
}
}
else
measure(w, h);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//super.setMeasuredDimension(1000,1000);
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
}我发现在视频开始播放之前,重写函数onMeasure(int, int)被多次调用,因此我删除了onMeasure(int, int)函数并调用:setMeasuredDimension(int, int) in side function:public void calculateAdditional(int w, int h){...},而不是使用measure(int, int),但它也不会影响任何事情。我能做些什么来调整视频的大小,使之与精确的比率完全吻合。
此外,尝试将创建视频视图的布局参数更改为、FILL_PARENT、或,但始终存在错误。
发布于 2015-06-22 05:30:00
使用FILL_PARENT代替:
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);https://stackoverflow.com/questions/30972521
复制相似问题