【问题标题】:MediaPlayer Video Size Codenameone?MediaPlayer 视频大小代号一个?
【发布时间】:2018-09-19 22:52:12
【问题描述】:

我一直在使用代号为 1 的 VideoCapture

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

一旦我们尝试在 MediaPlayer 组件中打开媒体时遇到问题,大多数时候视频对于屏幕来说显得太小,有时它会适合表单,

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

问候,

【问题讨论】:

标签: android ios video cross-platform codenameone


【解决方案1】:

这将适用于设备(​​不是模拟器),但我会推荐这个 API:

video.setNativePlayerMode(true);

【讨论】:

  • 我不是在模拟器上测试,我是在安卓设备上测试,除了我已经添加了这一行,结果还是一样
  • 你能提供你看到的截图吗?您是否在调用 start() 之前添加了该行?
【解决方案2】:

因为您写道您的问题与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);
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多