gpt4 book ai didi

java - 使用Java创建视频播放器

转载 作者:搜寻专家 更新时间:2023-10-31 19:37:39 25 4
gpt4 key购买 nike

我需要为我的项目使用 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();
}
}
}
}

最佳答案

我用了vlcj它工作顺利。它是与 vlcj 播放器的 java 绑定(bind),而且您不必提供任何驱动器,因为 vlcj 已经将所有驱动器包含在二进制分发版中。

试一试,有 example of already working player为你打造!

关于java - 使用Java创建视频播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34037909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com