Reputation: 418
i am trying to embedded a video player to a JFrame as follow when user click on the JMenu then click on a JMenuItem (open video) the JFileChooser pop up and ask hem to select video and remove the JTextFile to the right side and set the video to the left side i did all of that but i don't know how to put the video into Canvas because it Always gives error so i need the write way because i deleted all the error line so code here are not giving any error i have 2 class the first class for the gui and the second is the one i write here can any one help me about what to write in the actionlistener and my attached class
this is the video Operation class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package animeaid;
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.runtime.RuntimeUtil;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
/**
*
* @author isslam
*/
public class VideoOpration {
public static Canvas c;
VideoOpration() {
MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
c = new Canvas();
c.setBackground(Color.black);
EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
mediaPlayer.playMedia(GuiInterface.mediaPath);
}
}
the class of the gui that declare the JFileChoosear
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package AnimeAid;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import javax.swing.table.*;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
/**
*
* @author isslam
*/
public class GuiInterface extends JFrame {
private final JTable table;
private final JTextField enterText;
private final JMenu jMenu1,jMenu2,jMenu3;
private final JMenuBar jMenuBar1;
private final JMenuItem itemNewSrt,itemOpenVideo;
private static JFileChooser ourFileSelector;
File ourFile;
public static String mediaPath="";
String vlcPath="C:\\Program Files\\VideoLAN\\VLC";
public GuiInterface(String title){
setSize(1024, 720);
setTitle(title);
setDefaultCloseOperation(GuiInterface.EXIT_ON_CLOSE);
String[] columnNames = {"#","Start","End","Translation column"};
Object[][] data = {
{"1", "00:00:01,600","00:00:04,080", "Mr Magnussen, please state your\n" +
"full name for the record."},
{"2", "00:00:04,080 ","00:00:07,040","Charles Augustus Magnussen."}};
enterText = new JTextField();
ourFileSelector = new JFileChooser();
jMenuBar1 = new JMenuBar();
jMenu1 = new JMenu("File");
jMenu2 = new JMenu("Video");
jMenu3 = new JMenu("Subtitle");
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
jMenuBar1.add(jMenu3);
itemNewSrt = new JMenuItem("this text only");
jMenu1.add(itemNewSrt);
itemOpenVideo = new JMenuItem("Open Video");
jMenu2.add(itemOpenVideo);
setJMenuBar(jMenuBar1);
table = new JTable(data, columnNames);
table.setFillsViewportHeight(true);
table.setAutoResizeMode( JTable.AUTO_RESIZE_ALL_COLUMNS );
TableColumn columnA = table.getColumn("#");
columnA.setMinWidth(10);
columnA.setMaxWidth(20);
TableColumn columnB= table.getColumn("Start");
columnB.setMinWidth(80);
columnB.setMaxWidth(90);
TableColumn columnC= table.getColumn("End");
columnC.setMinWidth(80);
columnC.setMaxWidth(90);
JPanel textFiled = new JPanel(new GridBagLayout());
GridBagConstraints co = new GridBagConstraints();
co.fill = GridBagConstraints.HORIZONTAL;
co.gridx =0;
co.gridy =0;
co.weightx=0.5;
co.weighty=1;
co.gridheight=0;
co.gridwidth=0;
co.ipadx=900;
co.ipady=80;
co.anchor = GridBagConstraints.PAGE_START;
co.insets = new Insets(100, 0, 5, 0);
textFiled.add(enterText,co);
JPanel p = new JPanel();
p.add(VideoOpration.c);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
add(textFiled, BorderLayout.NORTH);
add(p, BorderLayout.WEST);
//Container cp = getContentPane();
//cp.add(videoCon);
itemOpenVideo.addActionListener(new MenuBarMethod());
}
public class MenuBarMethod implements ActionListener{
@Override
public void actionPerformed(ActionEvent a){
Object buttonPressed=a.getSource();
if(buttonPressed.equals(itemOpenVideo)){
ourFileSelector.setFileSelectionMode(JFileChooser.FILES_ONLY);
ourFileSelector.showSaveDialog(null);
ourFile = ourFileSelector.getSelectedFile();
mediaPath = ourFile.getAbsolutePath();
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlcPath);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}
}
}
}
Upvotes: 0
Views: 649
Reputation: 4146
Your video surface Canvas is being added to a JPanel.
JPanel by default has a FlowLayout, and FlowLayout lays out components according to their preferred size. Your Canvas has no preferred size so it won’t appear. Either set a more appropriate layout manager on your panel, like BorderLayout with CENTER constraint, or give your Canvas a size with setSize().
You have a further problem...
Your MediaPlayerFactory and EmbeddedMediaPlayer references will go out of scope when your constructor exits and will therefore become eligible for garbage collection. When that happens your video playback will stop. You must keep those objects pinned to prevent garbage collection (usually declaring them as fields in the class will accomplish this, but you must make sure the class containing those references does not go out of scope too).
In fact, your "VideoOpration" class looks redundant to me, your code would be clearer without it.
And finally a fundamental problem with your approach is that you really shouldn’t share static variables between those two classes to exchange information. You know basics like invoking methods and passing variables, right?
You really should look at the many examples provided with vlcj at [1], and in particular [2].
[1] https://github.com/caprica/vlcj/tree/master/src/test/java/uk/co/caprica/vlcj/test
Upvotes: 1