Trying to embed vlcj media player in WindowsCanvas inside JPanel

I am trying to play a video using vlcj inside JPanel, but this does not work for me. The exception to the message I receive is "java.lang.IllegalStateException: the video surface component should be displayed", which is the same problem as in Continue to receive the error, "The component must be displayed" .

The JPanel code that contains the canvas and vlcj player is this:

import javax.swing.JPanel; import com.sun.jna.Native; import com.sun.jna.NativeLibrary; import java.awt.Canvas; import java.awt.Color; import uk.co.caprica.vlcj.binding.LibVlc; import uk.co.caprica.vlcj.player.MediaPlayerFactory; import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer; import uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface; import uk.co.caprica.vlcj.runtime.RuntimeUtil; import uk.co.caprica.vlcj.runtime.windows.WindowsCanvas; public class MyJPanel extends JPanel { private EmbeddedMediaPlayer player; private WindowsCanvas canvas; public MyJPanel() { canvas = new WindowsCanvas(); add(canvas); revalidate(); repaint(); canvas.setVisible(true); MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(); player = mediaPlayerFactory.newEmbeddedMediaPlayer(); CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas); player.setVideoSurface(videoSurface); player.playMedia("v.avi"); // This sentence throws the exception. } } 

MyJFrame extends JFrame and contains only MyJPanel JPanel. I think this is not important at all.

 import javax.swing.JFrame; public class MyJFrame extends JFrame { protected MyJPanel myJPanel; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MyJFrame frame = new MyJFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public MyJFrame() { myJPanel = new myJPanel(); add(myJPanel); } } 

Thanks in advance.

+4
source share
1 answer

You are trying to play media before the frame containing the canvas is visible. You will need to place the call to playMedia() in a separate method and call it after creating and displaying the entire frame.

EDIT:

If you still want it to play directly, just call the appropriate method after creating it and make your frame visible:

 MyJFrame frame = new MyJFrame(); frame.setVisible(true); frame.startPlaying(); 

... obviously, you need to define startPlaying() in MyJFrame, but then it should start playing straight. you just need to set the visible frame first.

+6
source

All Articles