Java Swing视频播放器

时间:2016-08-16 09:29:11

标签: java swing video media-player

我需要一个Java视频播放器,在启动时打开文件浏览器,然后在Swing / Gui窗口中打开一个选定的视频文件。我搜索了分配,但却找不到合适的分配。我确实发现了类似于此处发布的代码,但我无法启动它,因为它说Player无法解析为类型,它无法识别ControllerListener等等...如果我可以获得简单视频播放器的代码源或某些帮助我真的很感激。

package program;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class MediaPlayerDemo extends JFrame 
{
     private Player player;
     private File file;

     public MediaPlayerDemo()
     {
         super( "Demonstrating the Java Media Player" );

         JButton openFile = new JButton( "Open file to play" );
         openFile.addActionListener( new ActionListener() 
         {
             public void actionPerformed( ActionEvent e )
             {
                 openFile();
                 createPlayer();
             }
         });
         getContentPane().add( openFile, BorderLayout.NORTH );

         setSize( 300, 300 );
         show();
     }

     private void openFile()
     {
         JFileChooser fileChooser = new JFileChooser();

         fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
         int result = fileChooser.showOpenDialog( this );

         // user clicked Cancel button on dialog
         if ( result == JFileChooser.CANCEL_OPTION )
             file = null;
         else
             file = fileChooser.getSelectedFile();
     }

     private void createPlayer()
     {
         if ( file == null )
             return;

         removePreviousPlayer();

         try 
         {
             // create a new player and add listener
             player = Manager.createPlayer( file.toURL() );
             player.addControllerListener( new EventHandler() );
             player.start(); // start player
         }
         catch ( Exception e )
         {
             JOptionPane.showMessageDialog( this, "Invalid file or location", "Error loading file",
             JOptionPane.ERROR_MESSAGE );
         }
     }

     private void removePreviousPlayer()
     {
         if ( player == null )
             return;

         player.close();

         Component visual = player.getVisualComponent();
         Component control = player.getControlPanelComponent();

         Container c = getContentPane();

         if ( visual != null )
             c.remove( visual );

         if ( control != null )
             c.remove( control );
     }

     public static void main(String args[])
     {
         MediaPlayerDemo app = new MediaPlayerDemo();

         app.addWindowListener( new WindowAdapter() 
         {
             public void windowClosing( WindowEvent e )
             {
                 System.exit(0);
             }
         });
     }

     // inner class to handler events from media player
     private class EventHandler implements ControllerListener 
     {
         public void controllerUpdate( ControllerEvent e ) 
         {
             if ( e instanceof RealizeCompleteEvent ) 
             {
                 Container c = getContentPane();

                 // load Visual and Control components if they exist
                 Component visualComponent = player.getVisualComponent();

                 if ( visualComponent != null )
                     c.add( visualComponent, BorderLayout.CENTER );

                 Component controlsComponent = player.getControlPanelComponent();

                 if ( controlsComponent != null )
                     c.add( controlsComponent, BorderLayout.SOUTH );

                 c.doLayout();
             }
         }
     }
}

0 个答案:

没有答案