2014-01-23 54 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