1

I am trying to play the video in the java swing.All I can hear is the audio but video does not show up. I am using EmbeddedMediaPlayerComponent. All the buttons are showing up correctly on screen as well in the panel2.

I tried looking at the other threads and made updates to my code but it did not work. Can anyone help with that?

Update: I am using macosx

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;

import uk.co.caprica.vlcj.binding.RuntimeUtil;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;

public class MediaPlayer extends JPanel {

// Declares our media player component
private EmbeddedMediaPlayerComponent mediaplayer;
// This string holds the media URL path
private static String mediapath;
private String mediaPath="";
private JPanel panel,panel2;
private JButton play_btn, stop_btn, foward_btn, rewind_btn, enlarge_btn;
private JSlider timeline;

public MediaPlayer(String mediaPath) {

    this.mediaPath=mediaPath;

    NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "/Applications/VLC.app/Contents/MacOS/lib");
    mediaplayer = new EmbeddedMediaPlayerComponent();

    panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(mediaplayer, BorderLayout.CENTER);


    //New code
    play_btn = new JButton("play");
    stop_btn = new JButton("stop");
    foward_btn = new JButton("ff");
    rewind_btn = new JButton("rew");
    enlarge_btn = new JButton("enlarge");

    timeline = new JSlider(0, 100, 0);
    timeline.setMajorTickSpacing(10);
    timeline.setMajorTickSpacing(5);
    timeline.setPaintTicks(true);

    panel2 = new JPanel();
    panel2.setLayout(new FlowLayout(FlowLayout.CENTER));
    panel2.add(play_btn);
    panel2.add(stop_btn);
    panel2.add(foward_btn);
    panel2.add(rewind_btn);
    panel2.add(enlarge_btn);
    panel2.add(timeline);
    panel.add(panel2, BorderLayout.SOUTH);
    setLayout(new BorderLayout());
    add(panel, BorderLayout.CENTER);


}

public void play() {
        mediaplayer.mediaPlayer().media().play(mediaPath);
}

public static void main(String[] args) {


    mediapath = "/Users/name/Documents/Projects/Scenario2.mp4";
    MediaPlayer mediaplayer = new MediaPlayer(mediapath);
    JFrame ourframe = new JFrame("Sample");
    ourframe.setContentPane(mediaplayer);
    ourframe.setSize(840, 600);
    ourframe.setVisible(true);
    mediaplayer.play();
    ourframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

I expect to see the video in the Panel.

techworker
  • 11
  • 3
  • Have you tried using `SwingUtilities.invokeLater()` to build and run your UI? [Some](https://stackoverflow.com/q/7156949/7932271) [discussion](https://stackoverflow.com/q/491323/7932271). – DavidW Jun 18 '19 at 20:11
  • Hey David, I tried that as well but I get the same result. It also plays the audio without any video – techworker Jun 18 '19 at 20:15
  • most likely a duplicate of https://stackoverflow.com/questions/25638820/mac-os-jdk-1-8-problems-vlc-control-jawt-not-load/25651219#25651219 – caprica Jun 18 '19 at 21:08
  • @caprica, I checked the tagged thread but I didn't encounter JAWT loading issues. Can you have a look at the code posted for any causes? – techworker Jun 18 '19 at 21:27
  • The specific error may be different but the fundamental reason is most likely the same. OSX does not have heavyweight AWT and what you're doing requires heavyweight AWT – caprica Jun 19 '19 at 05:35
  • If audio works then VLCJ decoder is working. Test this [**MP4 video file**](http://techslides.com/demos/sample-videos/small.mp4) and see if that one displays a picture... – VC.One Jun 19 '19 at 06:58
  • @VC.One, I tried using the mp4 file you mentioned, still the same result – techworker Jun 20 '19 at 17:00
  • I'll try your code when I get a chance in the next 24 hrs (not on Java IDE). I'll be using Windows though. Has any example code from tutorials or StackOverflow search etc managed to display a video for you? Try: https://stackoverflow.com/q/13440760/2057709 – VC.One Jun 20 '19 at 18:20
  • @VC.One, thanks! and yes I tried the code in tutorial link you mentioned. It didn't work out too. – techworker Jun 25 '19 at 15:02

0 Answers0