MediaPlayer Video Size Codenameone?

蹲街弑〆低调 提交于 2021-02-07 20:43:14

问题


I have been working on VideoCapture with Codename one using

String file=Capture.captureVideo();
Media video = MediaManager.createMedia(file, true);
f.addComponent(BorderLayout.CENTER, video.getVideoComponent());
f.revalidate();

and once we do that the problem when we try to open the Media in a MediaPlayer component , the video appear too small for the screen most of the time , and sometime it get fit to the form ,

the question is : how can I adjust the media inside the MediaPlayer component to fill the whole form

Regards,


回答1:


This will work on the device (not simulator) but I would recommend this API:

video.setNativePlayerMode(true);



回答2:


Because you wrote that your question is related to Codename One - Zoom, center and crop a video to force it to occupy all the screen, I give you the code that solved my issue. It's an external layout manager, you can adapt it to your needs:

package ...;

import com.codename1.io.Log;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Display;
import com.codename1.ui.geom.Dimension;
import com.codename1.ui.layouts.Layout;

/**
 * This layout manager fits a MediaPlayer component to the available space,
 * cropping the video similar to "fit" scale in Codename One. It requires that
 * only one MediaPlayer component is added and that the video orientation of the
 * MediaPlayer data source is the same orientation of the device (thanks to an
 * external orientation listener). See:
 * https://stackoverflow.com/questions/49311206/codename-one-zoom-center-and-crop-a-video-to-force-it-to-occupy-all-the-scree
 *
 * @author Francesco Galgani
 */
public class AutoFitVideoLayout extends Layout {

    int smallerVideoSize;
    int longerVideoSize;

    /**
     * The costructor needs the width and the height of the video (the landscape
     * or portrait orientation doesn't matter)
     *
     * @param width
     * @param height
     */
    public AutoFitVideoLayout(int width, int height) {
        if (width < height) {
            smallerVideoSize = width;
            longerVideoSize = height;
        } else {
            smallerVideoSize = height;
            longerVideoSize = width;
        }
    }

    @Override
    public void layoutContainer(Container parent) {
        for (Component current : parent) {
            int videoWidth;
            int videoHeight;
            if (parent.getWidth() > parent.getHeight()) {
                // Landscape orientation
                videoWidth = parent.getWidth();
                videoHeight = Double.valueOf((double) (parent.getWidth()) * smallerVideoSize / longerVideoSize).intValue();
                if (videoHeight < parent.getHeight()) {
                    videoHeight = parent.getHeight();
                    videoWidth = Double.valueOf((double) (parent.getHeight()) * longerVideoSize / smallerVideoSize).intValue();
                }
            } else {
                // Portrait orientation
                videoWidth = parent.getWidth();
                videoHeight = Double.valueOf((double) (parent.getWidth()) * longerVideoSize / smallerVideoSize).intValue();
                if (videoHeight < parent.getHeight()) {
                    videoHeight = parent.getHeight();
                    videoWidth = Double.valueOf((double) (parent.getHeight()) * smallerVideoSize / longerVideoSize).intValue();
                }
            }

            current.setSize(new Dimension(videoWidth, videoHeight));
            current.setX((parent.getWidth() - current.getWidth()) / 2);
            current.setY((parent.getHeight() - current.getHeight()) / 2);

            Log.p("Screen size: " + Display.getInstance().getDisplayWidth() + " * " + Display.getInstance().getDisplayHeight());
            Log.p("Video size: " + videoWidth + " * " + videoHeight);
            Log.p("Video absolute position: " + current.getAbsoluteX() + ", " + current.getAbsoluteY());
            Log.p("Video parent absolute position: " + current.getParent().getAbsoluteX() + ", " + current.getParent().getAbsoluteY());
        }
    }

    @Override
    public Dimension getPreferredSize(Container parent) {
        if (parent.getWidth() > 0 && parent.getHeight() > 0) {
            return new Dimension(parent.getWidth(), parent.getHeight());
        } else {
            return new Dimension(100, 100);
        }
    }

}


来源:https://stackoverflow.com/questions/49751593/mediaplayer-video-size-codenameone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!