我尝试使用 GDX-video extension在 libGDX 项目中。我已经转换了MP4
文件写入OGG
(Theora+Vorbis) 使用 VLC
并置于res/data/
下android项目中的文件夹。
当我运行项目时,看到黑屏。
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.graphics.g3d.utils.MeshBuilder;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.video.VideoPlayer;
import com.badlogic.gdx.video.VideoPlayerCreator;
import java.io.FileNotFoundException;
public class MyGdxGame extends ApplicationAdapter implements InputProcessor {
public PerspectiveCamera cam;
public CameraInputController inputController;
public ModelInstance instance;
public Environment environment;
public VideoPlayer videoPlayer;
public Mesh mesh;
private final Vector3 tmpV1 = new Vector3();
private final Vector3 target = new Vector3();
@Override
public void create() {
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, .4f, .4f, .4f, 1f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(10f, 10f, 10f);
cam.lookAt(0, 0, 0);
cam.near = 0.1f;
cam.far = 300f;
cam.update();
MeshBuilder meshBuilder = new MeshBuilder();
meshBuilder.begin(Usage.Position | Usage.TextureCoordinates, GL20.GL_TRIANGLES);
// @formatter:off
meshBuilder.box(5, 5, 5);
// @formatter:on
mesh = meshBuilder.end();
videoPlayer = VideoPlayerCreator.createVideoPlayer(cam, mesh, GL20.GL_TRIANGLES);
try {
videoPlayer.play(Gdx.files.internal("data/converted.ogg"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Gdx.input.setInputProcessor(new InputMultiplexer(this, inputController = new CameraInputController(cam)));
Gdx.gl.glEnable(GL20.GL_CULL_FACE);
Gdx.gl.glCullFace(GL20.GL_BACK);
}
@Override
public void render() {
inputController.update();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
final float delta = Gdx.graphics.getDeltaTime();
tmpV1.set(cam.direction).crs(cam.up).y = 0f;
cam.rotateAround(target, tmpV1.nor(), delta * 20);
cam.rotateAround(target, Vector3.Y, delta * -30);
cam.update();
if (!videoPlayer.render()) { // As soon as the video is finished, we start the file again using the same player.
try {
videoPlayer.play(Gdx.files.internal("data/converted.ogg"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
@Override
public void dispose () {
}
public boolean needsGL20 () {
return true;
}
public void resume () {
}
public void resize (int width, int height) {
}
public void pause () {
}
@Override
public boolean keyDown(int keycode) {
return false;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
}
这是一个新创建的项目,只需Gradle
依赖项已添加到其中。
最佳答案
好吧,这是 5 年前提出的问题(大约),但我决定对于我当前的项目,我需要它与 LibGDX 一起工作。
首先关闭存储库页面:
https://github.com/libgdx/gdx-video
这个页面对我有一点帮助,可能不会再被提及:
https://github.com/libgdx/gdx-video/issues/2
然后我很好奇的是:
https://oss.sonatype.org/content/repositories/snapshots/com/badlogicgames/gdx-video/
注意:这是 2021 年 11 月,所以我为此使用的是当前版本的 IntelliJ Idea 和 LibGDX。
在开始集成此库之前,我将补充一点,LibGDX 仍将旧版本的 LWJGL for Desktop 设置为早期版本,但我通过将“DesktopLauncher.java”更改为以下内容来修复此问题:
//import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
//import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.mygdx.game.MyGdxGame;
public class DesktopLauncher {
public static void main (String[] arg) {
//LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
//new LwjglApplication(new MyGdxGame(), config);
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.setWindowedMode(1280, 768);
new Lwjgl3Application(new MyGdxGame(), config);
}
}
在第一页上给出了这样的解释:
Core:
implementation "com.badlogicgames.gdx-video:gdx-video:0.0.1"
Desktop:
implementation "com.badlogicgames.gdx-video:gdx-video-lwjgl:0.0.1"
or
implementation "com.badlogicgames.gdx-video:gdx-video-lwjgl3:0.0.1"
Android:
implementation "com.badlogicgames.gdx-video:gdx-video-android:0.0.1"
它在 github 页面上表示使用当前版本的“gdx-video”而不是“0.0.1”。上面的第三个链接是“gdx-video”的存储库,进入目录后,截至该日期的当前版本是“1.3.2-SNAPSHOT”。我花了几天时间才弄清楚我必须在末尾添加“-SNAPSHOT”。
因此,为了使其正常工作,“build.gradle”文件已更改为:
project(":core") {
apply plugin: "java-library"
// https://oss.sonatype.org/content/repositories/snapshots/com/badlogicgames/gdx-video/
dependencies {
implementation "com.badlogicgames.gdx-video:gdx-video:1.3.2-SNAPSHOT"
api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
api "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
}
}
project(":android") {
apply plugin: "com.android.application"
configurations { natives }
dependencies {
implementation project(":core")
implementation "com.badlogicgames.gdx-video:gdx-video-android:1.3.2-SNAPSHOT"
api "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
api "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64"
api "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
}
}
project(":desktop") {
apply plugin: "java-library"
dependencies {
implementation project(":core")
implementation "com.badlogicgames.gdx-video:gdx-video-desktop:1.3.2-SNAPSHOT"
api "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"
api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
api "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
api "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
}
}
桌面项目随后构建并运行良好,但在 Android 上仍然存在问题。这是一个简单的修复,因为错误消息指出 minSdkVersion 需要为 16 而不是 14。
在“build.gradle:android”中:
defaultConfig {
applicationId "com.mygdx.game"
minSdkVersion 16 // just change this
targetSdkVersion 30
versionCode 1
versionName "1.0"
}
另请注意,您需要重新同步您的 gradle 项目。
所以,这已经为桌面和 Android 构建了它,我没有使用其他平台,但它应该与存储库中可用的内容相同。
另外,我正要开始测试它,所以我实际上不知道它是否有效。我会回来根据需要进行编辑。
更新:2021 年 11 月 28 日
我放弃了“gdx-video”,主要是因为将视频帧转换为与 LibGDX 兼容的纹理的开销。我过去使用过 ffmpeg,并使用自定义着色器来渲染 YUV 视频帧,而不是先转换为 RGB。这在低端 Android 硬件上播放 1080p 视频时最为明显,视频播放非常不稳定。使用我的旧代码,我能够在单核 800Mhz Arm 芯片上玩 1080p 60FPS。该版本在四核 ARM 芯片上运行 25FPS 时遇到了困难。
还有“gdx-video”的桌面版本,虽然可以构建,但缺少 VideoPlayerDesktop 类并且无法播放任何内容。
所以对我来说,它是回到绘图板并设置两个单独的项目。
关于java - libGDX 中的视频播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35940543/