MediaPlayer视频大小Codenameone是什么?

3

我一直在使用Codename One进行VideoCapture的工作。

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

一旦我们这样做了,当我们尝试在MediaPlayer组件中打开媒体时,视频大多数时间会显示得太小,有时它会适应表单,

问题是:我如何调整MediaPlayer组件内的媒体以填满整个表单?

祝好!


1
你在使用什么代码来在MediaPlayer组件中播放媒体?你的问题是否与以下问题相关? https://stackoverflow.com/questions/49311206/codename-one-zoom-center-and-crop-a-video-to-force-it-to-occupy-all-the-scree - Francesco Galgani
1
@FrancescoGalgani,是的,同样的概念。 - AhMaD AbUIeSa
2个回答

1
这将在设备上运行(而不是模拟器),但我建议使用此API:
video.setNativePlayerMode(true);

我不是在模拟器上测试,我是在安卓设备上测试,另外我已经添加了这行代码,结果还是一样。 - AhMaD AbUIeSa
你能提供一下你所看到的屏幕截图吗?在调用start()之前,你是否添加了那行代码? - Shai Almog

0

因为您提到您的问题与Codename One - 缩放、居中和裁剪视频以强制其占据整个屏幕有关,所以我给您提供解决我的问题的代码。这是一个外部布局管理器,您可以根据自己的需求进行调整:

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

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接