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).

1  
sclass JFFMPEGVideoPlayer extends JSelfSwingable {
2  
  //SurfaceShowingAutoZoomedImage imageSurface;
3  
  ImageSurface imageSurface;
4  
  JPanel buttons = rightAlignedLine();
5  
  JButton btnPlayPause = jDisabledButton("Pause", r playPause);
6  
  
7  
  ImageStreamFromVideoFile stream;
8  
  long playStartTime;
9  
  new BoolVar paused;
10  
  
11  
  RSTVar<BufferedImage> imageToDisplay = new(image -> imageSurface.setImage(image));
12  
  
13  
  gettable File videoFile;
14  
  
15  
  event videoStarted(File videoFile);
16  
  event userSelectedVideo(File videoFile);
17  
  
18  
  void init {
19  
    //imageSurface = swing(-> new SurfaceShowingAutoZoomedImage);
20  
    imageSurface = doubleBufferedImageSurface();
21  
    imageSurface.setAutoZoomToDisplay(true);
22  
    buttons.add(btnPlayPause);
23  
    buttons.add(jbutton("Open video...", rThread openDialog));
24  
    bindToComponent(imageSurface, null, rThread closeStream);
25  
    
26  
    setComponent(
27  
      centerAndSouth(
28  
        jscroll_center_borderless(imageSurface),
29  
        withMargin(buttons)
30  
      ));
31  
  }
32  
  
33  
  selfType standardZoom() { imageSurface.standardZoom(); this; }
34  
  
35  
  *() { init(); }
36  
  
37  
  *(File videoFile) {
38  
    init();
39  
    if (videoFile != null)
40  
      onFirstShow(imageSurface, -> play(videoFile));
41  
  }
42  
  
43  
  void openDialog swing {
44  
    new JFileChooser fc;
45  
    fc.setDialogTitle("Open video");
46  
    fc.setCurrentDirectory(dirToOpen());
47  
    File videoFile = execFileChooser(fc);
48  
    if (videoFile == null) ret;
49  
    print("openDialog " + nListeners(onUserSelectedVideo));
50  
    userSelectedVideo(videoFile);
51  
    play(videoFile);
52  
  }
53  
  
54  
  File dirToOpen() {
55  
    if (videoFile != null) ret dirOfFile(videoFile);
56  
    ret videosDir();
57  
  }
58  
  
59  
  void play(File videoFile) {
60  
    printCommaCombine("JFFMPEGVideoPlayer.play " + videoFile,
61  
      nListeners(onVideoStarted));
62  
    if (!isFile(videoFile))
63  
      ret with print("Not a file");
64  
      
65  
    thread { // TODO: use something better than just a new thread
66  
      if (videoFile == null) ret;
67  
      JFFMPEGVideoPlayer.this.videoFile = videoFile;
68  
      pcall-infobox {
69  
        closeStream();
70  
        stream = new ImageStreamFromVideoFile;
71  
        stream.open(videoFile);
72  
        stream.onHaveImage((image, pos) -> {
73  
          waitWhileTrue(paused);
74  
          
75  
          if (playStartTime == 0 || playStartTime+pos.toMillis() < sysNow())
76  
            playStartTime = sysNow()-pos.toMillis();
77  
          else
78  
            sleepUntilSysTime(playStartTime+pos.toMillis());
79  
          possiblyDisplay(image);
80  
        });
81  
        paused.set(false);
82  
        enableButton(btnPlayPause);
83  
        stream.startDecodeThread();
84  
        videoStarted(videoFile);
85  
      }
86  
    }
87  
  }
88  
  
89  
  void possiblyDisplay(BufferedImage image) {
90  
    imageToDisplay.set(cloneBufferedImage(image));
91  
  }
92  
  
93  
  void closeStream {
94  
    disableButton(btnPlayPause);
95  
    possiblyDisplay(whiteImage(10));
96  
    dispose stream;
97  
    playStartTime = 0;
98  
  }
99  
  
100  
  void playPause() swing {
101  
    paused.set(!paused!);
102  
    setText(btnPlayPause, paused! ? "Play" : "Pause");
103  
  }
104  
}

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: 271 / 709
Version history: 47 change(s)
Referenced in: [show references]