libgdx播放视频、libgdx播放MP4、libgdx播放动画
转自:https://lingkang.top/archives/libgdx-bo-fang-shi-pin
转自:https://lingkang.top/archives/libgdx-bo-fang-shi-pin
转自:https://lingkang.top/archives/libgdx-bo-fang-shi-pin
最近《完蛋!我被美女包围了!》比较火,它基于视频交互进行游戏。于是有了这篇文章。
libgdx实现视频播放,是对视频进行帧拆解,通过渲染更新到画面上。
当前最新版本的库:2023年11月14日
使用的库,这是一个快照版。说明libgdx目前还未发版,说明还不够完善。
<!--有10MB+需要慢慢等待下载-->
<dependency>
<groupId>com.badlogicgames.gdx-video</groupId>
<artifactId>gdx-video-lwjgl3</artifactId>
<version>1.3.2-SNAPSHOT</version>
</dependency>
需要加入快照仓库
<repository>
<id>nexus1</id>
<name>Nexus1</name>
<layout>default</layout>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
可以在此处浏览
https://oss.sonatype.org/content/repositories/snapshots/com/badlogicgames/gdx-video/
不同环境的格式注意
在window中,如果没有webM格式视频,百度一个在线转换器:
mp4转webm在线转换器
package video;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.video.VideoPlayer;
import com.badlogic.gdx.video.VideoPlayerCreator;
import org.junit.Test;
import java.io.FileNotFoundException;
/**
* @author lingkang
* created by 2023/11/14
*/
public class MyVideo extends ApplicationAdapter {
@Test
public void test() {
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.setForegroundFPS(30);
config.setTitle("yzcy");
config.setWindowedMode(800, 600);
new Lwjgl3Application(this, config);
}
VideoPlayer player;
protected SpriteBatch batch;
@Override
public void create() {
player = VideoPlayerCreator.createVideoPlayer();
FileHandle file = Gdx.files.absolute("C:\\Users\\Administrator\\Desktop\\video.webm");
try {
player.play(file);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
System.out.println("加载完成");
batch=new SpriteBatch();
}
@Override
public void render() {
player.update();
batch.begin();
batch.draw(player.getTexture(),0,0);
batch.end();
}
@Override
public void dispose() {
player.dispose();
}
}
效果
完整依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.lingkang</groupId>
<artifactId>yzcy</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<gdx.version>1.12.0</gdx.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx</artifactId>
<version>${gdx.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-backend-lwjgl3 -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx-backend-lwjgl3</artifactId>
<version>${gdx.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-platform -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx-platform</artifactId>
<version>${gdx.version}</version>
<classifier>natives-desktop</classifier>
</dependency>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-freetype -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx-freetype</artifactId>
<version>${gdx.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-freetype-platform -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx-freetype-platform</artifactId>
<version>${gdx.version}</version>
<classifier>natives-desktop</classifier>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<!--有10MB+需要慢慢等待下载-->
<dependency>
<groupId>com.badlogicgames.gdx-video</groupId>
<artifactId>gdx-video-lwjgl3</artifactId>
<version>1.3.2-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<!-- 编译后保持方法形参名称不变 -->
<!--<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>-->
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>tencent</id>
<name>tencent</name>
<layout>default</layout>
<url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>nexus1</id>
<name>Nexus1</name>
<layout>default</layout>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>nexus</id>
<name>Nexus</name>
<layout>default</layout>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>aliyunmaven</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>