Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

android - Resize video to fit the VideoView

I'm positioning a VideoView with AbsoluteLayout (I need to display it on several places into the screen at specific positions).

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

But the Video ins't getting resized to the bounds I specified.

Any tips?

Another related question is, how to make the MediaController show over the VideoView?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

this is very close to what you are trying. if you are willing to subclass the videoview and override onMeasure here is the solution. you need to

MyVideoView extends VideoView

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


as for showing MediaController over VidowView

Declare

private MediaController controller = null;

OnCreate

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

Activity

@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;
}

Media Controller events

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

a sample layout file

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...