我是java编程的初学者。我将在java中播放视频,但卡住了。 首先我尝试了vlcj,但它只适用于32位JDK(是不是?)现在我正在使用mpg文件尝试JMF。 问题是我只能听到文件的声音,看不到视频。它显示:无法处理格式:MPEG,640x360,FrameRate = 29.9,长度= 345600 我正在使用的代码是:
import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MediaPlayer extends JPanel {
public MediaPlayer(URL mediauUrl) {
setLayout(new BorderLayout());
try {
Player mediaPlayer=Manager.createRealizedPlayer(new MediaLocator(mediauUrl));
Component video=mediaPlayer.getVisualComponent();
Component control=mediaPlayer.getControlPanelComponent();
if (video!=null) {
add(video, BorderLayout.CENTER); // place the video component in the panel
}
add(control, BorderLayout.SOUTH); // place the control in panel
mediaPlayer.start();
} catch (Exception e) {
}
}
public static void main(String[] args) {
URL mediaUrl=null;
File file = new File("src/media/Jellyfish.mpg");
try {
mediaUrl = file.toURL();
} catch (MalformedURLException ex) {
System.out.println(ex);
}
JFrame mediaTest = new JFrame( "Movie Player" );
mediaTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
MediaPlayer mediaPanel = new MediaPlayer( mediaUrl );
mediaTest.add( mediaPanel );
mediaTest.setSize( 800, 700 ); // set the size of the player
mediaTest.setLocationRelativeTo(null);
mediaTest.setVisible( true );
}
}
有人可以帮忙吗?提前致谢