调整视频大小以适应VideoView

时间:2011-08-11 19:20:59

标签: android android-video-player

我正在使用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();
}

但视频没有调整到我指定的范围。

任何提示?

另一个相关的问题是,如何通过VideoView使MediaController显示?

3 个答案:

答案 0 :(得分:16)

这与你正在尝试的非常接近。如果您愿意继承视频视图,override onMeasure这里是解决方案。你需要

MyVideoView extends VideoView

https://stackoverflow.com/questions/4434027/android-videoview-orientation-change-with-buffered-video/4452597#4452597


显示 MediaController over VidowView

声明

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>

答案 1 :(得分:10)

您只能更改布局文件:

<?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>

答案 2 :(得分:2)

更改LayoutParams后,我认为您需要致电setLayoutParams,以便使用新的参数更新布局。调用setLayoutParams后,您可能需要调用方法reqestLayout以使布局无效并触发重绘视图。

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