Will
Will

Reputation: 3585

How can I play Video Files using VLCJ and JavaFX?

I'm aware that JavaFX has it's own media player, but I do not know if it can play MP4 files. Even if it could, I would still prefer to use VLCJ as VLC supports more formats and varieties than I can count in my near-catatonic state.

I've followed the example posted by Caprical in his VLCJ-JavaFX GitHub but it does, well, nothing. It doesn't error, but it does nothing.

Looking into the code, it seems the issue is in the Timeline event handler:

private final EventHandler<ActionEvent> nextFrame = new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent t) {
        Memory[] nativeBuffers = mediaPlayerComponent.getMediaPlayer().lock();
        if (nativeBuffers != null) { //<-----This is always NULL so everything in the block is skipped . . . 
            // FIXME there may be more efficient ways to do this...
            // Since this is now being called by a specific rendering time, independent of the native video callbacks being
            // invoked, some more defensive conditional checks are needed
            Memory nativeBuffer = nativeBuffers[0];
            if (nativeBuffer != null) {
                ByteBuffer byteBuffer = nativeBuffer.getByteBuffer(0, nativeBuffer.size());
                BufferFormat bufferFormat = ((DefaultDirectMediaPlayer) mediaPlayerComponent.getMediaPlayer()).getBufferFormat();
                if (bufferFormat.getWidth() > 0 && bufferFormat.getHeight() > 0) {
                    pixelWriter.setPixels(0, 0, bufferFormat.getWidth(), bufferFormat.getHeight(), pixelFormat, byteBuffer, bufferFormat.getPitches()[0]);
                }
            }
        }
        mediaPlayerComponent.getMediaPlayer().unlock();
    };
};

It's been suggested I get the logs but that will have to wait as I'm slipping into unconsciousness as I make this post (when I return to the land of the living, I will see about what I can do to post some). If there is a better way to make this happen, I'm all for it if someone can direct me there. Thanks...

Upvotes: 1

Views: 2500

Answers (1)

caprica
caprica

Reputation: 4146

The vlcj-javafx sample on the Github project works just fine.

You say in your question that vlcj "doesn't error, but it does nothing".

Well, there are two ways to check for errors which you don't show in the code you posted in your question.

  1. The mediaPlayer.playMedia() method returns a boolean to say whether VLC accepted your MRL or not - did you check the return value? Note that even if this method returns true, it does not categorically mean VLC could play your media, but if it returns false it categorically means it could not be played.
  2. You should add a MediaPlayerEventLister to your media player and provide implementations for "playing()" and "error()". These callbacks will be triggered asynchronously - because that's how LibVLC works - only then can you conclude whether vlcj "doesn't error", or "does nothing".

I suspect your media failed to start, probably because of a wrong filename.

Upvotes: 1

Related Questions