【发布时间】:2014-06-21 21:44:00
【问题描述】:
我已经遇到这个问题好几天了,找不到解决方案。 问题是,当我在 Eclipse 中运行我的程序时,我的介绍视频会显示并播放,但是当我将其导出到可运行的 jar 中时,视频不会加载,只有 jframe 会加载。
我正在使用带有 e(fx)clipse 的 eclipse。
在我的主类中,我只是创建一个 Jframe 并将 MediaPanel 对象添加到 Jframe:
mediaPanel = new MediaPanel( "/videos/Composite2.avi" );
frame.getContentPane().add(mediaPanel);
这是我的 MediaPanel 类
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JPanel;
public class MediaPanel extends JPanel
{
Player mediaPlayer;
public MediaPanel( String mediaURL ) throws MalformedURLException
{
URL mediaURL1 = this.getClass().getResource(mediaURL);
System.out.println(mediaURL1);
setLayout( new BorderLayout() ); // use a BorderLayout
// Use lightweight components for Swing compatibility
Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );
try
{
// create a player to play the media specified in the URL
mediaPlayer = Manager.createRealizedPlayer( mediaURL1 );
// 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 )
{
System.err.println( "No media player found" );
} // end catch
catch ( CannotRealizeException cannotRealizeException )
{
System.err.println( "Could not realize media player" );
} // end catch
catch ( IOException iOException )
{
System.err.println( "Error reading from the source" );
} // end catch
} // end MediaPanel constructor
public void stop(){
mediaPlayer.stop();
}
} // end class MediaPanel
【问题讨论】:
-
什么是'System.out.println(mediaURL1);'显示?在 Eclipse 和 jar 上是一样的吗?你检查过视频吗?
-
"jar:file:/C:/Users/Sven/Desktop/Hackers%20Club/mata2.jar!/videos/Composite2.avi" 这是我的控制台打印的内容......也说没有找到媒体播放器。而在 eclipse "file:/C:/Users/Sven/workspace/HackersClub/bin/videos/Composite2.avi" 也许这只是一个愚蠢的错误,或者我太盲目了,看不到它的过度思考!
-
好的,现在,如果你用提取器打开罐子……根目录下的文件夹是videos吗?但是如果它说“没有找到媒体播放器”......也许你应该检查罐子里的库或者看看这个问题:stackoverflow.com/questions/11097174/playing-video-using-jmf
-
文件夹在那里,也尝试使用 .flv 文件。我的项目中仍然没有任何库,除了 jre8 库之外什么都没有。
-
解决了。我是个白痴,正试图从 jar 访问视频文件。 JMF 无法做到这一点。感谢上帝。感谢您的支持
标签: java swing video jar javafx