2014-01-23 43 views
0

我想将视频播放器嵌入到JFrame中,如下图所示,当用户单击JMenu时,然后单击JMenuItem(打开的视频)JFileChooser弹出并询问下摆选择视频,并将JTextFile删除到右侧,并将视频设置到左侧我做了所有这些,但我不知道如何将视频放入Canvas,因为它总是给出错误,所以我需要写入方式,因为我删除了所有的错误行,所以这里的代码没有给出任何错误,我有2类贵宾的第一类,第二个是我写在这里的任何人可以帮助我在actionlistener和我附加的类写什么如何使用所选格式的Canvas播放视频JFileChooser

这是视频操作类

/* 
* 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); 

     } 

} 

类GUI的那个声明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); 

     } 
    } 

} 

} 
+0

* “它总是给错误” * 1)总是复制/粘贴错误&异常输出。 2)为了更快地获得更好的帮助,请发布[MCVE](http://stackoverflow.com/help/mcve)。 - BTW - 类名'VideoOpration'应该是'VideoOperation'以正确拼写。 –

+0

这个代码没有错误的事情是我不知道如何把视频在jpanel中的主框架 –

+0

@peeskillet你可以看看这个代码 –

回答

1

您的视频面帆布被添加到一个JPanel。

JPanel默认情况下有一个FlowLayout,而FlowLayout根据它们的首选大小来布局组件。您的画布没有首选尺寸,因此不会显示。在面板上设置一个更合适的布局管理器,比如带有CENTER约束的BorderLayout,或者使用setSize()给Canvas一个大小。

你有进一步的问题...

你MediaPlayerFactory和EmbeddedMediaPlayer引用将走出去的范围时你的构造函数退出,会因此变得符合垃圾回收。当这种情况发生时,视频播放将停止。您必须保留这些对象以防止垃圾回收(通常将它们声明为类中的字段将完成此操作,但您必须确保包含这些引用的类不会超出范围)。

事实上,您的“VideoOpration”类对我来说看起来多余,您的代码在没有它的情况下会更清晰。

最后,你的方法的一个基本问题是,你真的不应该在这两个类之间共享静态变量来交换信息。你知道像调用方法和传递变量这样的基础知识,对吗?

你真的应该看看vlcj提供的许多例子[1],特别是[2]。

[1] https://github.com/caprica/vlcj/tree/master/src/test/java/uk/co/caprica/vlcj/test

[2] https://github.com/caprica/vlcj/blob/master/src/test/java/uk/co/caprica/vlcj/test/basic/TestPlayer.java