如何使用JMF在JFrame上快速启动视频



谁能告诉我在JPanel中播放视频的最佳方式是什么?我目前正在做一款儿童学习软件,选择后会播放一些学习视频。视频存储在某个文件夹中。我已经成功地完成了使用JMF的工作,但问题是它有点慢。虽然视频不是太大(2-4 MB),但启动视频需要一些时间。我怎么做才能使它快速启动,

谢谢你

public class MediaPanel extends JPanel
{
public static boolean playing = false;
public static Player mediaPlayer;
public MediaPanel( URL mediaURL )
{
setLayout( new BorderLayout() ); // use a BorderLayout
// Use lightweight components for Swing compatibility
//Component controls = mediaPlayer.getControlPanelComponent();
} // end MediaPanel constructor
public void play(URL mediaURL) throws NoPlayerException, CannotRealizeException,               IOException, IncompatibleSourceException{
Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );
System.out.println("sdfdsg");
mediaPlayer = Manager.createRealizedPlayer( mediaURL );
Component video = mediaPlayer.getVisualComponent();
if ( video != null )
add( video);
mediaPlayer.start();
playing=true;
}
public void stop(){
mediaPlayer.stop();
playing = false;
}
public void play_one(){
mediaPlayer.stop();
mediaPlayer.start();
}
}
public class MediaPanel extends JPanel
{
public static boolean playing = false;
public static Player mediaPlayer;
public MediaPanel( URL mediaURL )
{
setLayout( new BorderLayout() ); 
} // end MediaPanel constructor
public void play(URL mediaURL) throws NoPlayerException, CannotRealizeException,     IOException, IncompatibleSourceException{
Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );
mediaPlayer = Manager.createRealizedPlayer( mediaURL );
Component video = mediaPlayer.getVisualComponent();
if ( video != null )
add( video);
mediaPlayer.start();
playing=true;
}
public void stop(){
mediaPlayer.stop();
playing = false;
}
public void play_one(){
mediaPlayer.stop();
mediaPlayer.start();
}
}

和我使用这个类的源代码部分..

try {
Home.text.setText("wait..");
if(Home.mediaPanel.playing==true)
Home.mediaPanel.stop();
Home.mediaPanel.play(new     URL("file://C://Users//zubair//workspace//learningKit//"+Home.selected_topic+"//"+Home.sele cted_group+"//v"+k+".flv"));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

JMF来自石器时代。你有坚持下去的理由吗?

我建议使用JavaFX,它支持媒体,包括视频播放。JavaFX是Java 8的一部分,也随Java 7一起发布(只是不在默认类路径上)。您可以播放支持的视频格式(*)。Flv, *.mp4)只需要几行代码,如果可用,它还使用硬件加速。

基本上你只需要创建一个Media, MediaPlayerMediaView类:

Media m = new Media(Paths.get("example.flv").toURI().toString());
MediaPlayer mp = new MediaPlayer(m);
MediaView mv = new MediaView(mp);
// Add the mediaview component somewhere to your GUI
// And you're done. You can start playing the video by:
mp.play();

这里是一个简短的,完整的示例应用程序,播放视频:http://www.java2s.com/Code/Java/JavaFX/FullScreenVideoPlayer.htm

Oracle官方教程:

JavaFX Media简介

相关内容

  • 没有找到相关文章

最新更新