Resize video to fit the VideoView
Asked Answered
S

3

19

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?

Sister answered 11/8, 2011 at 19:20 Comment(1)
You need to resize the video not the view. Try to find some video editor library for thatWatt
R
16

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://mcmap.net/q/330811/-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>
Ruche answered 29/8, 2011 at 2:16 Comment(3)
My biggest problem is not to resize the VideoView, is to resize the Video that was playing inside of it. I want to it assume all the space I specified to the VideoView.Sister
does not resizing the videoview resize the video aswell? the link i have shown you does change the size of the video in the overriden onMeasure. What i'm missing.Ruche
Well, this actually resizes, but I was tricked by the XML that comes and make an area to the View on a aspect ratio that the Video won't support. Also, I already tried to add the MediaController by this way, but it's shown over the Activity.Sister
B
16

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>
Bicuspid answered 31/3, 2013 at 12:6 Comment(2)
It's well explained in blog.kasenlam.com/2012/02/…Traceable
This worked for me. I need to add android:layout_gravity="center"Elyot
N
2

After you change your LayoutParams I think you will need to call setLayoutParams so the layout can be updated with the new params. After the call to setLayoutParams you may have to call the method reqestLayout to invalidate the layout and trigger a redraw of the view.

You will need to create a new instance of the MediaController and use the method setMediaController on the VideoView object.

Nosedive answered 12/8, 2011 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.