18

我正在使用 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 上?

4

3 回答 3

16

这与您正在尝试的非常接近。如果您愿意将视频视图子类化,override onMeasure这就是解决方案。你需要

MyVideoView extends VideoView

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


至于在 VidowView 上显示 MediaController

宣布

private MediaController controller = null;

创建时

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>
于 2011-08-29T02:16:11.140 回答
13

You can only change the layout file:

<?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>
于 2013-03-31T12:06:57.657 回答
2

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

您将需要创建一个新实例并在对象上MediaController使用该方法。setMediaControllerVideoView

于 2011-08-12T19:31:37.350 回答