我使用框架布局,我在上面绑定我的视频播放器:
private void initVideoPlayer(View root) {
mVideoPlayer = new TextureViewVideoPlayer(width, height);
mVideoPlayer.setOnErrorListener(this);
mVideoPlayer.setOnPreparedListener(this);
mVideoPlayer.setOnCompletionListener(this);
mVideoPlayer.setOnBufferingUpdateListener(this);
mVideoPlayer.setOnSeekCompleteListener(this);
mVideoPlayer.setOnVideoSizeChangedListener(this);
mVideoPlayer.bindView((FrameLayout) root.findViewById(R.id.videoFrame), 0);
}
这是来自TextureViewVideoPlayer的onVideoSizeChanged函数:
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
Log.i(TAG, "onVideoSizeChanged " + width + " ZZ " + height + " Display Height: " + displayheight + " --- REAL VIDEO RATIO: -- " + ((double) width / (double) height));
if (this.mOnVideoSizeChangedListener != null)
this.mOnVideoSizeChangedListener.onVideoSizeChanged(this, width, height);
if (width == 0 || height == 0) {
mp.release();
Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
return;
}
double ratio = ((double) width / (double) height);
if (displayheight > height) {
ratio = ((double) displayheight / (double) height);
} else {
ratio = ((double) height / (double) displayheight);
}
LayoutParams params = new FrameLayout.LayoutParams((int) (width * ratio), (int) (height * ratio));
// LayoutParams params = new FrameLayout.LayoutParams((width),
// (height));
Log.i(TAG, "onVideoSizeChanged mod" + params.width + " ZZ " + params.height + " RATIO: " + ratio + " --- REAL RATIO: -- " + ((double) params.width / (double) params.height));
params.gravity = Gravity.CENTER;
mTextureView.setLayoutParams(params);
this.mTextureView.requestLayout();
}
正如您所看到的,它需要视频的视频宽度和高度,它会计算比率,然后它会占用显示器的高度,然后按比例查看视频比屏幕大多少(或更小) ,然后它使用该比率为视频创建参数,并在textureView上设置此参数。
在xml中,绑定我的播放器的VideoFrame也有:
<FrameLayout
android:id="@+id/videoFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >
</FrameLayout>
现在,如果我保持这样,视频会被拉伸,即使它看起来像一个矩形,视频顶部和底部的一部分也会消失(宽度和高度相同的1:1) ,屏幕高度越大。现在,如果我不使用比率,并使用视频的宽度和高度创建参数(640 x 480)它将是一个小方块。 (比例1:1),并拉长。有什么想法可以避免这种情况吗?
PS:我的纹理视图实际上是一个方形纹理视图,它有这个onMeasure函数:
@Override
protected void onMeasure(int paramInt1, int paramInt2) {
int i = View.MeasureSpec.getSize(paramInt1);
setMeasuredDimension(i, i);
}
我已将其更改为:
@Override
protected void onMeasure(int paramInt1, int paramInt2) {
int i = View.MeasureSpec.getSize(paramInt1);
int j = View.MeasureSpec.getSize(paramInt2);
setMeasuredDimension(i, j);
}
希望这可以解决我的问题。
答案 0 :(得分:0)
我的纹理视图有这个:
@Override
protected void onMeasure(int paramInt1, int paramInt2) {
int i = View.MeasureSpec.getSize(paramInt1);
setMeasuredDimension(i, i);
}
要修复它,我已将其更改为:
@Override
protected void onMeasure(int paramInt1, int paramInt2) {
int i = View.MeasureSpec.getSize(paramInt1);
int j = View.MeasureSpec.getSize(paramInt2);
setMeasuredDimension(i, j);
}