Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created tool implementation of gdx-video for testing
- Loading branch information
1 parent
438f26d
commit fe7cdc4
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
description = 'Play videos from MPQ archives.' | ||
application.mainClass = 'com.riiablo.video.VideoPlayerTool' | ||
|
||
dependencies { | ||
implementation "com.badlogicgames.gdx:gdx-jnigen:$gdxVersion" | ||
implementation "com.badlogicgames.gdx-video:gdx-video:1.3.2-SNAPSHOT" | ||
implementation "com.badlogicgames.gdx-video:gdx-video-lwjgl:1.3.2-SNAPSHOT" | ||
} |
148 changes: 148 additions & 0 deletions
148
tools/video-player/src/main/java/com/riiablo/video/VideoPlayerTool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package com.riiablo.video; | ||
|
||
import java.io.FileNotFoundException; | ||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.Option; | ||
import org.apache.commons.cli.Options; | ||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
|
||
import com.badlogic.gdx.Application; | ||
import com.badlogic.gdx.Gdx; | ||
import com.badlogic.gdx.files.FileHandle; | ||
import com.badlogic.gdx.graphics.GL20; | ||
import com.badlogic.gdx.graphics.Texture; | ||
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | ||
import com.badlogic.gdx.utils.Array; | ||
import com.badlogic.gdx.video.VideoPlayer; | ||
import com.badlogic.gdx.video.VideoPlayerCreator; | ||
|
||
import com.riiablo.Riiablo; | ||
import com.riiablo.logger.Level; | ||
import com.riiablo.logger.LogManager; | ||
import com.riiablo.logger.Logger; | ||
import com.riiablo.mpq.MPQFileHandleResolver; | ||
import com.riiablo.tool.LwjglTool; | ||
import com.riiablo.tool.Tool; | ||
import com.riiablo.util.InstallationFinder; | ||
|
||
public class VideoPlayerTool extends Tool { | ||
private static final Logger log = LogManager.getLogger(VideoPlayerTool.class); | ||
|
||
public static void main(String[] args) { | ||
LogManager.setLevel(VideoPlayerTool.class.getCanonicalName(), Level.TRACE); | ||
LwjglTool.create(VideoPlayerTool.class, "video-player", args) | ||
.title("Video Player Tool") | ||
.size(640, 480, false) // default video size | ||
.start(); | ||
} | ||
|
||
FileHandle home; | ||
String file; | ||
|
||
float canvasX, canvasY; | ||
float canvasWidth, canvasHeight; | ||
|
||
SpriteBatch batch; | ||
VideoPlayer player; | ||
|
||
@Override | ||
protected String getHelpHeader() { | ||
return "Plays a specified D2 video file.\n" + | ||
"E.g., {cmd} --file data/local/video/BlizNorth640x480.bik"; | ||
} | ||
|
||
@Override | ||
protected void createCliOptions(Options options) { | ||
super.createCliOptions(options); | ||
options.addOption(Option | ||
.builder("f") | ||
.longOpt("file") | ||
.desc("path to the video file to play") | ||
.required() | ||
.hasArg() | ||
.argName("path") | ||
.build()); | ||
} | ||
|
||
@Override | ||
protected void handleCliOptions(String cmd, Options options, CommandLine cli) { | ||
super.handleCliOptions(cmd, options, cli); | ||
|
||
InstallationFinder finder = InstallationFinder.getInstance(); | ||
Array<FileHandle> homeDirs = finder.getHomeDirs(); | ||
home = homeDirs.first(); | ||
|
||
String fileOptionValue = cli.getOptionValue("file"); | ||
file = fileOptionValue; | ||
log.trace("file={}", file); | ||
} | ||
|
||
@Override | ||
public void create() { | ||
Gdx.app.setLogLevel(Application.LOG_DEBUG); | ||
Riiablo.home = home = Gdx.files.absolute(home.path()); | ||
Riiablo.mpqs = new MPQFileHandleResolver(); | ||
|
||
batch = new SpriteBatch(); | ||
player = VideoPlayerCreator.createVideoPlayer(); | ||
player.setOnCompletionListener(new VideoPlayer.CompletionListener() { | ||
@Override | ||
public void onCompletionListener(FileHandle file) { | ||
log.info("finished playing " + file.name()); | ||
} | ||
}); | ||
player.setOnVideoSizeListener(new VideoPlayer.VideoSizeListener() { | ||
@Override | ||
public void onVideoSize(float width, float height) { | ||
log.info("size " + width + "x" + height); | ||
canvasWidth = width; | ||
canvasHeight = height <= 240 ? height * 2 : height; | ||
canvasY = Gdx.graphics.getHeight() / 2 - canvasHeight / 2; | ||
} | ||
}); | ||
Gdx.app.postRunnable(new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
// FileHandle handle = Riiablo.mpqs.resolve(file); | ||
// if (handle == null) throw new FileNotFoundException(file); | ||
FileHandle handle = Gdx.files.absolute(file); | ||
player.play(handle); | ||
player.setVolume(0.1f); | ||
log.info("playing " + handle); | ||
} catch (FileNotFoundException t) { | ||
log.error(ExceptionUtils.getRootCauseMessage(t), t); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void render() { | ||
Gdx.gl.glClearColor(0f, 0f, 0f, 0f); | ||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | ||
player.update(); | ||
batch.begin(); | ||
Texture frame = player.getTexture(); | ||
if (frame != null) { | ||
batch.draw(frame, canvasX, canvasY, canvasWidth, canvasHeight); | ||
} | ||
batch.end(); | ||
} | ||
|
||
@Override | ||
public void pause() { | ||
player.pause(); | ||
} | ||
|
||
@Override | ||
public void resume() { | ||
player.resume(); | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
batch.dispose(); | ||
player.dispose(); | ||
} | ||
} |