调整视频大小以适应VideoView

19

我正在使用AbsoluteLayout来定位一个VideoView(我需要将其显示在屏幕的特定位置上)。

public void showVideo(RectF bounds, final String videoUrl) {
    AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams) video.getLayoutParams();
    params.width = (int) bounds.width();
    params.height = (int) bounds.height();
    params.x = (int) bounds.left;
    params.y = (int) bounds.top;

    video.requestLayout();

    video.setVisibility(View.VISIBLE);
    video.setFocusable(true);
    video.setFocusableInTouchMode(true);
    video.requestFocus();

    File file = new File(videoUrl);
    video.setVideoPath(file.getAbsolutePath());
    video.start();
}

但是视频没有被调整到我指定的边界尺寸。有什么建议吗?

另一个相关问题是如何使MediaController显示在VideoView上方?


你需要调整视频大小而不是视图。尝试寻找一些视频编辑库来实现。 - IgniteCoders
3个回答

16

这非常接近你想要的。如果你愿意子类化VideoView并且在此覆盖onMeasure,那么这就是解决方案。你需要

MyVideoView extends VideoView

https://dev59.com/p2855IYBdhLWcg3wSSKl#4452597


关于如何在VidowView上方显示MediaController

声明

private MediaController controller = null;

OnCreate

controller = new MediaController(this);
controller.setMediaPlayer(mcEvents);
controller.setAnchorView(findViewById(R.id.wrapper));

活动

@Override
public boolean onTouchEvent(MotionEvent event) {
    //the MediaController will hide after 3 seconds - tap the screen to make it appear again
    controller.show();
    return false;
}

媒体控制器事件

private MediaController.MediaPlayerControl mcEvents = new MediaController.MediaPlayerControl() {        
    public void start() {
        player.start();
    }

    public void seekTo(int pos) {
        player.seekTo(pos);         
    }

    public void pause() {
        player.pause();
    }

    public boolean isPlaying() {            
        return player.isPlaying();
    }

    public int getDuration() {          
        return player.getDuration();
    }

    public int getCurrentPosition() {           
        return player.getCurrentPosition();
    }

    public int getBufferPercentage() {          
        return pBuffer;
    }

    public boolean canSeekForward() {           
        return true;
    }

    public boolean canSeekBackward() {
        return true;
    }

    public boolean canPause() {
        return true;
    }
};

一个示例布局文件。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.my.views.MyViedoView
    android:id="@+id/player"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
/>
</FrameLayout>

我的最大问题不是调整VideoView的大小,而是调整其中播放的视频的大小。我希望它能够占据我指定给VideoView的所有空间。 - Marcos Vasconcelos
不调整VideoView的大小也会调整视频大小吗?我向您展示的链接确实在重写onMeasure时更改了视频大小。我错过了什么。 - Samuel
好的,这实际上调整了大小,但我被XML所欺骗,使得View在一个视频不支持的宽高比下呈现。此外,我已经尝试通过这种方式添加MediaController,但它显示在Activity之上。 - Marcos Vasconcelos

15

您只能更改布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <VideoView android:id="@+id/videoViewRelative"
         android:layout_alignParentTop="true"
         android:layout_alignParentBottom="true"
         android:layout_alignParentLeft="true"
         android:layout_alignParentRight="true"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
    </VideoView>

</RelativeLayout>

1
这在http://blog.kasenlam.com/2012/02/android-how-to-stretch-video-to-fill.html中有很好的解释。 - Vincent
这对我有用。我需要添加android:layout_gravity="center"。 - Joe Aspara

2

在更改LayoutParams后,您需要调用setLayoutParams方法,以便使用新的参数更新布局。在调用setLayoutParams方法之后,您可能需要调用requestLayout方法来使布局失效并触发视图的重新绘制。

您需要创建一个MediaController的新实例,并在VideoView对象上使用setMediaController方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接