Java 向JPanel显示视频

Java 向JPanel显示视频,java,netbeans,jpanel,jmf,Java,Netbeans,Jpanel,Jmf,我正在创建一个简单的视频播放器,但在JPanel中显示要流的视频文件时遇到了问题。我创建并设计了一个JFrame,并在表单中放置了一个正方形的JPanel 以下是我目前的代码: package SoundsTrip; import java.awt.BorderLayout; import java.awt.Component; import java.io.IOException; import java.net.MalformedURLException; import java.net.

我正在创建一个简单的视频播放器,但在JPanel中显示要流的视频文件时遇到了问题。我创建并设计了一个JFrame,并在表单中放置了一个正方形的JPanel

以下是我目前的代码:

package SoundsTrip;

import java.awt.BorderLayout;
import java.awt.Component;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author jmoreno
 */
public class VideoFrame extends javax.swing.JFrame {
    /** Creates new form VideoFrame */
    public VideoFrame() {
        initComponents();
        //this.setExtendedState(VideoFrame.MAXIMIZED_BOTH);
        this.setSize(650, 500);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 450, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 330, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel1);
        jPanel1.setBounds(10, 10, 450, 330);

        jButton1.setText("Open Video/Movie");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(470, 10, 160, 23);

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        try {
            openMedia();
        } catch (IOException ex) {
            Logger.getLogger(SoundBytePlaying.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CannotRealizeException ex) {
            Logger.getLogger(SoundBytePlaying.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void openMedia() throws IOException, CannotRealizeException{
        JFileChooser fileChooser = new JFileChooser();
        int result = fileChooser.showOpenDialog(null);
        if(result == JFileChooser.APPROVE_OPTION){
            URL mediaURL = null;
            try{
                mediaURL = fileChooser.getSelectedFile().toURL();
            }catch(MalformedURLException malformedURLException){
                JOptionPane.showMessageDialog(null, "Could not create URL for the file");
            }
            if(mediaURL != null){
                **showVideo() //some error here**
            }
        }
    }

    public void showVideo(URL mediaURL){
        Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );

        try{
            //create a player to play the media specified in the URL
            Player mediaPlayer = Manager.createRealizedPlayer( mediaURL );

            //get the components for the video and the playback controls
            Component video = mediaPlayer.getVisualComponent();
            Component controls = mediaPlayer.getControlPanelComponent();

            if ( video != null )
                add( video, BorderLayout.CENTER ); //add video component
            if ( controls != null )
                add( controls, BorderLayout.SOUTH ); //add controls

                mediaPlayer.start(); //start playing the media clip
        } //end try
        catch ( NoPlayerException noPlayerException ){
            JOptionPane.showMessageDialog(null, "No media player found");
        } //end catch
        catch ( CannotRealizeException cannotRealizeException ){
            JOptionPane.showMessageDialog(null, "Could not realize media player.");
        } //end catch
        catch ( IOException iOException ){
            JOptionPane.showMessageDialog(null, "Error reading from the source.");
        } //end catch
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VideoFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration

}
包装声带;
导入java.awt.BorderLayout;
导入java.awt.Component;
导入java.io.IOException;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.media.CannotRealizeException;
导入javax.media.Manager;
导入javax.media.NoPlayerException;
导入javax.media.Player;
导入javax.swing.JFileChooser;
导入javax.swing.JOptionPane;
/**
*
*@作者jmoreno
*/
公共类VideoFrame扩展了javax.swing.JFrame{
/**创建新形式的视频帧*/
公共视频帧(){
初始化组件();
//这个.setExtendedState(VideoFrame.MAXIMIZED_两者);
这个。设置大小(650500);
}
/**此方法从构造函数中调用,以
*初始化表单。
*警告:请勿修改此代码。此方法的内容为
*始终由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
// 
私有组件(){
jPanel1=newjavax.swing.JPanel();
jButton1=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
javax.swing.GroupLayout jPanel1Layout=新的javax.swing.GroupLayout(jPanel1);
setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,450,短。最大值)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,330,短。最大值)
);
getContentPane().add(jPanel1);
jPanel1.setBounds(10,10,450,330);
jButton1.setText(“开放视频/电影”);
jButton1.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.立根(470,10,160,23);
包装();
}// 
私有void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
试一试{
openMedia();
}捕获(IOEX异常){
Logger.getLogger(SoundBytePlaying.class.getName()).log(Level.SEVERE,null,ex);
}捕获(CannotRealizeException ex){
Logger.getLogger(SoundBytePlaying.class.getName()).log(Level.SEVERE,null,ex);
}
}
public void openMedia()引发IOException,CannotRealizeException{
JFileChooser fileChooser=新的JFileChooser();
int result=fileChooser.showOpenDialog(null);
if(result==JFileChooser.APPROVE\u选项){
URL mediaURL=null;
试一试{
mediaURL=fileChooser.getSelectedFile().toURL();
}捕获(异常异常异常异常异常异常异常){
showMessageDialog(null,“无法为文件创建URL”);
}
if(mediaURL!=null){
**showVideo()//此处出现一些错误**
}
}
}
公共void showVideo(URL mediaURL){
Manager.setHint(Manager.LIGHTWEIGHT\u渲染器,true);
试一试{
//创建播放机以播放URL中指定的媒体
Player mediaPlayer=Manager.createRealizedPlayer(mediaURL);
//获取视频和播放控件的组件
组件视频=mediaPlayer.getVisualComponent();
Component controls=mediaPlayer.getControlPanelComponent();
如果(视频!=null)
添加(视频,BorderLayout.CENTER);//添加视频组件
if(控件!=null)
添加(控件,BorderLayout.SOUTH);//添加控件
mediaPlayer.start();//开始播放媒体剪辑
}//结束尝试
捕获(NoPlayerException NoPlayerException){
showMessageDialog(null,“未找到媒体播放器”);
}//端盖
捕获(CannotRealizeException CannotRealizeException){
showMessageDialog(null,“无法实现媒体播放器”);
}//端盖
捕获(IOException IOException){
showMessageDialog(null,“从源读取时出错”);
}//端盖
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
invokeLater(new Runnable()){
公开募捐{
新视频帧().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JButton jButton1;
私有javax.swing.JPanel jPanel1;
//变量结束声明
}

非常感谢任何能让我来到这里的想法和帮助……:)

尝试将
视频和控件
组件添加到
jPanel1

...
if ( video != null )
 jPanel1.add( video, BorderLayout.CENTER ); //add video component
if ( controls != null )
 jPanel1.add( controls, BorderLayout.SOUTH ); //add controls
...

在测试基于JMF的项目时,请确保使用“JMF兼容”介质,这可能是在my上获得的。JMF非常古老,不支持用于最近制作的媒体的许多媒体类型或编解码器。

我真的建议使用JFM以外的任何一种,并在画布中显示视频。vlcj非常简单,下面是获取视频窗格的代码

String path = "/usr/lib"; //path for the vlc libs in linux
NativeLibrary.addSearchPath ( "libvlc", path );
System.setProperty ( "jna.library.path", path );

MediaPlayerFactory factory = new MediaPlayerFactory ();
mediaPlayer = factory.newEmbeddedMediaPlayer ();
mediaPlayer.setRepeat ( false );
mediaPlayer.setEnableKeyInputHandling ( false );
mediaPlayer.setEnableMouseInputHandling ( false );

CanvasVideoSurface videoSurface = factory.newVideoSurface ( canvas );
mediaPlayer.setVideoSurface ( videoSurface );
mediaPlayer.playMedia ( "/media/path/" );
请尝试以下代码:

if(mediaURL != null)
           {
                showVideo(mediaURL) //some error here**
            }

我应该用什么样的包装?你能给我一些想法,我可以看看和研究。。。谢谢。媒体自由:兼容JMF。如果您是Windows,您也可以考虑DirectShow包装器:但是不管您如何打开它,多媒体不是java的。