0

我使用帧布局,在其上绑定我的视频播放器,如下所示:

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();
}

如您所见,它获取视频的视频宽度和高度,计算比例,然后获取显示高度,并制作比例以查看视频比屏幕大(或小)多少,然后它使用该比率创建视频的参数,并在纹理视图上设置此参数。

在 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);
}

希望这可以解决我的问题。

4

1 回答 1

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);

}

于 2013-08-07T08:47:27.030 回答