Closed gastdozent91 closed 5 years ago
and especially #205
The short answer is you simply can't use the default EmbeddedMediaPlayer
on OSX with recent JDK versions because AWT on OSX is no longer heavyweight.
Consider using a CallbackMediaPlayerComponent
or CallbackVideoSurface
, or OpenGL rendering if you can use pre-release vlcj 5.x and pre-release VLC 4.x.
The situation on OSX sucks, and has done for a long time unfortunately.
The following projects at this github repo may be viable alternatives for you:
https://github.com/caprica/vlcj-javafx https://github.com/caprica/vlcj-lwjgl-demo https://github.com/caprica/vlcj-swt https://github.com/caprica/vlcj-swt-swing https://github.com/caprica/vlcj-swt-demo
Thanks for the advices! Finally it works with following test as template: https://github.com/caprica/vlcj/blob/master/src/test/java/uk/co/caprica/vlcj/test/window/WindowSurfaceTest.java
Here my new Code for all the couch potatoes out there ;)
JFrame jFrame = new JFrame("Window Video Surface");
jFrame.setBounds(100, 100, 800, 600);
jFrame.setBackground(Color.red);
jFrame.getContentPane().setBackground(Color.blue);
JPanel jPanel = new JPanel();
jPanel.setBackground(Color.green);
jFrame.getContentPane().setLayout(new BorderLayout());
jFrame.getContentPane().add(jPanel);
final MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
final EmbeddedMediaPlayer embeddedMediaPlayer = mediaPlayerFactory.mediaPlayers().newEmbeddedMediaPlayer();
Window window = new Window(jFrame);
window.setBackground(Color.gray);
VideoSurface videoSurface = mediaPlayerFactory.videoSurfaces().newVideoSurface(window);
embeddedMediaPlayer.videoSurface().set(videoSurface);
window.setBounds(100, 100, 800, 600);
window.setIgnoreRepaint(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
embeddedMediaPlayer.controls().stop();
embeddedMediaPlayer.release();
mediaPlayerFactory.release();
System.exit(0);
}
});
jFrame.setVisible(true);
window.setVisible(true);
embeddedMediaPlayer.media().prepare(VIDEO_URL);
embeddedMediaPlayer.media().play(VIDEO_URL);
Now try resizing your window...
That solution for OSX is "OK" but not ideal. It is sub-optimal for all other platforms.
On the other hand, if you don't care about resizes, it is probably good enough.
This works because Window is heavyweight in OSX AWT, whereas your original problem is that other components like Canvas are not heavyweight in OSX AWT (but they are in Linux/Windows).
If you'd have just used a "Callback" component/video surface, it would work cross-platform and have fully working resize behaviour, at the expense of some slightly worse performance.
Final comment on your code, prepare() is not needed before play(), as that play method intrinsically performs prepare() anyway.
Prepare is used when you do NOT want to play the media immediately.
Simple demo:
import uk.co.caprica.vlcj.player.component.CallbackMediaPlayerComponent;
import javax.swing.*;
public class SimpleVLCPlayer {
public static void main(String[] args) {
JFrame frame = new JFrame("VLC Player");
CallbackMediaPlayerComponent mediaPlayerComponent = new CallbackMediaPlayerComponent();
frame.setContentPane(mediaPlayerComponent);
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
mediaPlayerComponent.mediaPlayer().media().play("path to MP4");
}
}
Hey there,
i just try to make a simple video run .. it runs, but just with audio and without video (black screen). I tested the same code on Windows and it works. Any Ideas?
Here my code:
Setup: