2

Воспроизвожу видео следующим образом:

    uri = Uri.parse(arrayList.get(0));
    videoView = (VideoView) findViewById(R.id.videoView);
    videoView.setVideoURI(uri);
    videoView.start();

Мне нужно ограничить размеры (ширина/высота) видео квадратом, т.е. окно должно быть квадратным, но при этом видео должно правильно сжиматься в размерах, чтобы всё красиво выглядело. Пробовал через LayoutParams, но не работает.

1 ответ 1

2
public class MyVideoView extends VideoView {
private int mVideoWidth;
private int mVideoHeight;

public MyVideoView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyVideoView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public MyVideoView(Context context) {
    super(context);
}


@Override
public void setVideoURI(Uri uri) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource(this.getContext(), uri);
    mVideoWidth = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
    mVideoHeight = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
    super.setVideoURI(uri);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // Log.i("@@@", "onMeasure");
    int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
    int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
    if (mVideoWidth > 0 && mVideoHeight > 0) {
        if (mVideoWidth * height > width * mVideoHeight) {
            // Log.i("@@@", "image too tall, correcting");
            height = width * mVideoHeight / mVideoWidth;
        } else if (mVideoWidth * height < width * mVideoHeight) {
            // Log.i("@@@", "image too wide, correcting");
            width = height * mVideoWidth / mVideoHeight;
        } else {
            // Log.i("@@@", "aspect ratio is correct: " +
            // width+"/"+height+"="+
            // mVideoWidth+"/"+mVideoHeight);
        }
    }
    // Log.i("@@@", "setting size: " + width + 'x' + height);
    setMeasuredDimension(width, height);
    }
}

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.