当前位置:  开发笔记 > 编程语言 > 正文

使用Java创建视频播放器

如何解决《使用Java创建视频播放器》经验,为你挑选了1个好方法。

我需要为我的项目使用Java创建一个视频播放器.我已经在互联网上查了很多例子.其中一些运行,但没有显示任何屏幕,我只能听到视频的声音.请帮我解决这个问题......

我使用以下导入

import javax.media.*;

编辑:下面是我使用的代码:

import java.awt.*;
 import java.awt.event.*;
 import java.io.*;
 import javax.swing.*;
 import javax.media.*;

 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();
             }
         }
     }
 }

Petro Semeni.. 6

我使用了vlcj,它工作顺利.它是java绑定到vlcj播放器的好东西,你不必提供任何驱动器,因为vlcj已经包含所有它们的二进制分发.

试一试,有为您建造的已经工作的播放器的例子!



1> Petro Semeni..:

我使用了vlcj,它工作顺利.它是java绑定到vlcj播放器的好东西,你不必提供任何驱动器,因为vlcj已经包含所有它们的二进制分发.

试一试,有为您建造的已经工作的播放器的例子!

推荐阅读
oDavid_仔o_880
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有