BrightcoveVideoView
when working with the Native SDK for Android.Overview
As a developer, you can change the default behavior for how the Video is displayed on the Video View. You can resize/stretch the inner Video View (SurfaceView
/TextureView
) to fill the BrightcoveVideoView
(FrameLayout
). To do this, you can listen for the EventType.VIDEO_SIZE_KNOWN
event, do some math and set the display values.
There are two ways to change the Video View:
Resizing the container
You may want to develop an app that sets the Video View to fit the entire device screen in portrait mode. You can do this by resizing the container (BaseVideoView
/FrameLayout
).
Set the container property
If you see black margins around your video, you can try setting the BrightcoveVideoView
layout-height
.
- Open the XML layout file.
- Set the
BrightcoveVideoView layout-height
towrap_content
. - You may experience an initial flicker as explained in the note above.
Set the Video View size
Another way to control the Video View is to set the size programmatically.
- To minimize the initial flicker, try setting the the
layout_height
to a specific value (e.g. 280dp). The goal is for the height set to be close to the calculated height. - Listen for the
EventType.VIDEO_SIZE_KNOWN
event. - Get the
Event.VIDEO_WIDTH
andEvent.VIDEO_HEIGHT
- With those values, determine the aspect ratio.
- Use the aspect ratio to calculate the width and height on the display.
This solution will look similar to this:
//Event sent when we know the Video dimensions.
eventEmitter.on(EventType.VIDEO_SIZE_KNOWN, new EventListener() {
@Override
public void processEvent(Event event) {
// Get the width and height
float width = event.getIntegerProperty(Event.VIDEO_WIDTH);
float height = event.getIntegerProperty(Event.VIDEO_HEIGHT);
float aspectRatio = height/width;
//Get the display metrics
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int videoWidth = metrics.widthPixels; // We cover the entire display's width
int videoHeight = (int) (videoWidth*aspectRatio); //Calculate the height based on the ratio
// Set the layout params
brightcoveVideoView.setLayoutParams(new FrameLayout.LayoutParams(videoWidth,videoHeight));
}
});
When adding a View, you will need to override the layout params. For details, see the Android: Adding View/Managing Layout document.
Resizing the video
You may want to implement a double-tap that fills the screen. This would take a 16:9 video and fill the screen on an 18:9 or 19:9 device.
You can do this by resizing the actual video (SurfaceView
).
-
If you set the width and the height to the
renderView
to match the size of the device display, the video will resize to match the entire screen.//Event sent when we know the Video dimensions. eventEmitter.on(EventType.VIDEO_SIZE_KNOWN, new EventListener() { @Override public void processEvent(Event event) { // Get the width and height float width = event.getIntegerProperty(Event.VIDEO_WIDTH); float height = event.getIntegerProperty(Event.VIDEO_HEIGHT); float aspectRatio = height/width; //Get the display metrics DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int videoWidth = metrics.widthPixels; // We cover the entire display's width int videoHeight = (int) (videoWidth*aspectRatio); //Calculate the height based on the ratio // Set the video size brightcoveVideoView.getRenderView().setVideoSize(videoWidth, videoHeight); } });
-
The code below updates the underlying video view (e.g.
SurfaceView
) to match its parentBaseVideoView
(FrameLayout
). There are several things to keep in mind:-
You need to listen for the
VIDEO_SIZE_KNOWN
event when entering and exiting full screen, and when rotating the device. - You need to decide when to update your view for the above conditions.
One scenario could be that you set the
BaseVideoView layout_height
to280dp
and then resize theSurfaceView
to match280dp
. The aspect ratio will be modified slightly. If a user goes to full screen in portrait mode andupdateVideoViewMatchToParent()
is called, then both theBaseVideoView
andSurfaceView
will fill the device’s display and the video aspect ratio will be slightly modified.public void updateVideoViewMatchToParent(@NonNull BaseVideoView brightcoveVideoView) { RenderView renderView = brightcoveVideoView.getRenderView(); if (renderView != null) { renderView.setVideoSize(brightcoveVideoView.getWidth(), brightcoveVideoView.getHeight()); } }
To detect the double tap, you can use the GestureDetector.OnDoubleTapListener interface. For details, see the Detect common gestures document.
-
You need to listen for the
If you want to stretch the video without taking the aspect ratio into account (without cropping the video), you can try this:
eventEmitter.on(EventType.VIDEO_SIZE_KNOWN, new EventListener() {
@Override
public void processEvent(Event event) {
//Get the display metrics
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int videoHeight = metrics.heightPixels;
int videoWidth = metrics.widthPixels;
brightcoveVideoView.getRenderView().setVideoSize(videoWidth, videoHeight);
}
});
Keep in mind that the values you get are affected by the orientation of the device. To stretch the video for both portrait and landscape, you need to do the following:
-
Override the following
Activity
:public void onConfigurationChanged(Configuration configuration)
- Reset the video size.