MediaPlayer视频大小Codenameone?

时间:2018-04-10 10:42:30

标签: android ios video cross-platform codenameone

我一直使用

开发使用Codename的VideoCapture
String file=Capture.captureVideo();
Media video = MediaManager.createMedia(file, true);
f.addComponent(BorderLayout.CENTER, video.getVideoComponent());
f.revalidate();

一旦我们尝试在MediaPlayer组件中打开Media时出现问题,视频在大多数情况下对于屏幕显得太小,有时它适合表单,

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

此致

2 个答案:

答案 0 :(得分:1)

这可以在设备上运行(不是模拟器),但我会推荐这个API:

video.setNativePlayerMode(true);

答案 1 :(得分:0)

因为您写道,您的问题与Codename One - Zoom, center and crop a video to force it to occupy all the screen有关,所以我会向您提供解决我问题的代码。它是一个外部布局管理器,您可以根据自己的需要进行调整:

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

}