PageRenderTime 62ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/ch/vanwa/MinecraftCinema/VideoPlayer.java

https://gitlab.com/thallian/minecraft-cinema
Java | 150 lines | 116 code | 34 blank | 0 comment | 9 complexity | 46cdaac72dc55bc1145b48bcd402a03b MD5 | raw file
  1. package ch.vanwa.MinecraftCinema;
  2. import java.util.logging.Logger;
  3. import java.util.ArrayList;
  4. import java.io.File;
  5. import java.io.FilenameFilter;
  6. import java.awt.image.BufferedImage;
  7. import javax.imageio.ImageIO;
  8. import uk.co.caprica.vlcj.binding.LibVlc;
  9. import uk.co.caprica.vlcj.component.DirectMediaPlayerComponent;
  10. import uk.co.caprica.vlcj.discovery.NativeDiscovery;
  11. import uk.co.caprica.vlcj.player.MediaPlayer;
  12. import uk.co.caprica.vlcj.player.direct.BufferFormat;
  13. import uk.co.caprica.vlcj.player.direct.BufferFormatCallback;
  14. import uk.co.caprica.vlcj.player.direct.DirectMediaPlayer;
  15. import uk.co.caprica.vlcj.player.direct.RenderCallback;
  16. import uk.co.caprica.vlcj.player.direct.RenderCallbackAdapter;
  17. import uk.co.caprica.vlcj.player.direct.format.RV32BufferFormat;
  18. public final class VideoPlayer {
  19. protected static final Logger logger = Logger.getLogger("Minecraft.VideoPlayer");
  20. private final ArrayList<String> playlist = new ArrayList<String>();
  21. private int playlistPosition = 0;
  22. private static final int width = 125;
  23. private static final int height = 125;
  24. private final BufferedImage image;
  25. private final DirectMediaPlayerComponent mediaPlayerComponent;
  26. private final String[] mediaOptions;
  27. private ArrayList<VideoRenderer> renderers;
  28. public VideoPlayer(int audioPort) {
  29. boolean found = new NativeDiscovery().discover();
  30. BufferFormatCallback bufferFormatCallback = new BufferFormatCallback() {
  31. @Override
  32. public BufferFormat getBufferFormat(int sourceWidth, int sourceHeight) {
  33. return new RV32BufferFormat(width, height);
  34. }
  35. };
  36. this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  37. this.mediaPlayerComponent = new DirectMediaPlayerComponent(bufferFormatCallback) {
  38. @Override
  39. protected RenderCallback onGetRenderCallback() {
  40. return new VideoRenderCallbackAdapter();
  41. }
  42. @Override
  43. public void finished(MediaPlayer mediaPlayer) {
  44. next();
  45. }
  46. };
  47. this.mediaPlayerComponent.getMediaPlayer().setPlaySubItems(true);
  48. String vlcOptions = String.format("sout=#duplicate{dst=display,select=video,dst=es{access-audio=http,mux-audio=ogg,dst-audio=localhost:%s},select=audio}", audioPort);
  49. this.mediaOptions = new String[] { vlcOptions };
  50. initializePlaylist();
  51. play(0);
  52. this.renderers = new ArrayList<VideoRenderer>();
  53. }
  54. public void initializePlaylist() {
  55. this.playlist.clear();
  56. File videoDir = new File(CinemaPlugin.plugin.getDataFolder(), "videos");
  57. File[] videos = videoDir.listFiles(new FilenameFilter() {
  58. public boolean accept(File dir, String name) {
  59. return name.toLowerCase().endsWith(".webm");
  60. }
  61. });
  62. for (File video : videos) {
  63. this.playlist.add(video.getAbsolutePath());
  64. }
  65. }
  66. public void play() {
  67. this.mediaPlayerComponent.getMediaPlayer().play();
  68. }
  69. public void play(int position) {
  70. if (this.playlist.size() > 0) {
  71. String videoPath = this.playlist.get(position);
  72. this.mediaPlayerComponent.getMediaPlayer().playMedia(videoPath, this.mediaOptions);
  73. this.playlistPosition = position;
  74. }
  75. }
  76. public void pause() {
  77. this.mediaPlayerComponent.getMediaPlayer().pause();
  78. }
  79. public void next() {
  80. if (this.playlist.size() > 0) {
  81. if (this.playlistPosition == this.playlist.size() - 1) {
  82. this.playlistPosition = -1;
  83. }
  84. play(++this.playlistPosition);
  85. }
  86. }
  87. public void previous() {
  88. if (this.playlist.size() > 0) {
  89. if (this.playlistPosition == 0) {
  90. this.playlistPosition = this.playlist.size();
  91. }
  92. play(--this.playlistPosition);
  93. }
  94. }
  95. public String[] list() {
  96. if (this.playlist.size() > 0) {
  97. String[] playlistArray = new String[playlist.size()];
  98. playlistArray = this.playlist.toArray(playlistArray);
  99. return playlistArray;
  100. }
  101. return new String[] {};
  102. }
  103. public void register(VideoRenderer renderer) {
  104. this.renderers.add(renderer);
  105. renderer.setImage(this.image);
  106. }
  107. private class VideoRenderCallbackAdapter extends RenderCallbackAdapter {
  108. private VideoRenderCallbackAdapter() {
  109. super(new int[width * height]);
  110. }
  111. @Override
  112. protected void onDisplay(DirectMediaPlayer mediaPlayer, int[] rgbBuffer) {
  113. image.setRGB(0, 0, width, height, rgbBuffer, 0, width);
  114. }
  115. }
  116. }