Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

104
LINES

< > BotCompany Repo | #1034385 // JFFMPEGVideoPlayer

JavaX fragment (include) [tags: use-pretranspiled]

Uses 34444K of libraries. Click here for Pure Java version (21658L/136K).

sclass JFFMPEGVideoPlayer extends JSelfSwingable {
  //SurfaceShowingAutoZoomedImage imageSurface;
  ImageSurface imageSurface;
  JPanel buttons = rightAlignedLine();
  JButton btnPlayPause = jDisabledButton("Pause", r playPause);
  
  ImageStreamFromVideoFile stream;
  long playStartTime;
  new BoolVar paused;
  
  RSTVar<BufferedImage> imageToDisplay = new(image -> imageSurface.setImage(image));
  
  gettable File videoFile;
  
  event videoStarted(File videoFile);
  event userSelectedVideo(File videoFile);
  
  void init {
    //imageSurface = swing(-> new SurfaceShowingAutoZoomedImage);
    imageSurface = doubleBufferedImageSurface();
    imageSurface.setAutoZoomToDisplay(true);
    buttons.add(btnPlayPause);
    buttons.add(jbutton("Open video...", rThread openDialog));
    bindToComponent(imageSurface, null, rThread closeStream);
    
    setComponent(
      centerAndSouth(
        jscroll_center_borderless(imageSurface),
        withMargin(buttons)
      ));
  }
  
  selfType standardZoom() { imageSurface.standardZoom(); this; }
  
  *() { init(); }
  
  *(File videoFile) {
    init();
    if (videoFile != null)
      onFirstShow(imageSurface, -> play(videoFile));
  }
  
  void openDialog swing {
    new JFileChooser fc;
    fc.setDialogTitle("Open video");
    fc.setCurrentDirectory(dirToOpen());
    File videoFile = execFileChooser(fc);
    if (videoFile == null) ret;
    print("openDialog " + nListeners(onUserSelectedVideo));
    userSelectedVideo(videoFile);
    play(videoFile);
  }
  
  File dirToOpen() {
    if (videoFile != null) ret dirOfFile(videoFile);
    ret videosDir();
  }
  
  void play(File videoFile) {
    printCommaCombine("JFFMPEGVideoPlayer.play " + videoFile,
      nListeners(onVideoStarted));
    if (!isFile(videoFile))
      ret with print("Not a file");
      
    thread { // TODO: use something better than just a new thread
      if (videoFile == null) ret;
      JFFMPEGVideoPlayer.this.videoFile = videoFile;
      pcall-infobox {
        closeStream();
        stream = new ImageStreamFromVideoFile;
        stream.open(videoFile);
        stream.onHaveImage((image, pos) -> {
          waitWhileTrue(paused);
          
          if (playStartTime == 0 || playStartTime+pos.toMillis() < sysNow())
            playStartTime = sysNow()-pos.toMillis();
          else
            sleepUntilSysTime(playStartTime+pos.toMillis());
          possiblyDisplay(image);
        });
        paused.set(false);
        enableButton(btnPlayPause);
        stream.startDecodeThread();
        videoStarted(videoFile);
      }
    }
  }
  
  void possiblyDisplay(BufferedImage image) {
    imageToDisplay.set(cloneBufferedImage(image));
  }
  
  void closeStream {
    disableButton(btnPlayPause);
    possiblyDisplay(whiteImage(10));
    dispose stream;
    playStartTime = 0;
  }
  
  void playPause() swing {
    paused.set(!paused!);
    setText(btnPlayPause, paused! ? "Play" : "Pause");
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034385
Snippet name: JFFMPEGVideoPlayer
Eternal ID of this version: #1034385/48
Text MD5: 19e4ba25a5393112b494a457fc378501
Transpilation MD5: d155085d1c6e0536f366afb088fb54ff
Author: stefan
Category: javax / video
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-12 07:47:39
Source code size: 3054 bytes / 104 lines
Pitched / IR pitched: No / No
Views / Downloads: 269 / 707
Version history: 47 change(s)
Referenced in: [show references]