开发者

Show video to JPanel

I am creating a simple video player but I'm having problem to show the video file to stream in JPanel. I've created and designed a JFrame and put a square size JPanel inside the form.

Here is my codes so far:

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 开发者_如何学Cjavax.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

}

Many thanks to any idea and help I could get here... :)


Try to add your Video and Control components to your jPanel1

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


I would really suggest using vlcj either than JFM and showing video in canvas. vlcj is simple as pie, heres the code to get a video pane.

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


just try this code :

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


When testing JMF based projects, be sure to use 'JMF compatible' media as might be obtained at my media page. JMF is very old, and does not support many media types or CODECs used for media made recently.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜