加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 向JPanel显示视频

发布时间:2020-12-15 05:05:47 所属栏目:Java 来源:网络整理
导读:我正在创建一个简单的视频播放器,但我有问题显示要在JPanel中流式传输的视频文件.我创建并设计了一个JFrame,并在表单中放置了一个方形大小的JPanel. 到目前为止,这是我的代码: package SoundsTrip;import java.awt.BorderLayout;import java.awt.Component;
我正在创建一个简单的视频播放器,但我有问题显示要在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,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,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,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

}

非常感谢我能得到的任何想法和帮助……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读