我正在尝试使用jmf播放视频。经过几个小时的努力,删除了所有错误和异常,我得到了一个空指针异常。在查看了NullPointerException之后,我发现这应该是由于Player mediaPlayer的声明不正确,没有初始化为任何内容。
直接将其初始化为我稍后给出的值的另一个问题是,我也必须捕捉异常,然后它会说玩家可能不会被声明。
我如何声明mediaURL
和Player
,以便删除此nullpointerexception
并播放此视频。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.media.*;
import java.net.URL;
import java.io.*;
import java.net.MalformedURLException;
public class mediaPlayer extends JFrame
{
URL mediaURL;
Player mediaPlayer;
public mediaPlayer()
{
JFrame f = new JFrame("new");
f.setLayout(new BorderLayout());
f.setSize(500,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//file you want to play
try{
String name = "file:///"+new File("output.mp4").getAbsolutePath();
mediaURL = new URL(name);
}catch (MalformedURLException ex){}
//create the media player with the media url
try{
mediaPlayer = Manager.createRealizedPlayer(mediaURL);
}catch(IOException ex){} catch(NoPlayerException ex){} catch(CannotRealizeException ex){}
//get components for video and playback controls
Component video = mediaPlayer.getVisualComponent();
Component controls = mediaPlayer.getControlPanelComponent();
add(video,BorderLayout.CENTER);
add(controls,BorderLayout.SOUTH);
f.setVisible(true);
}
public static void main(String[] args){
new mediaPlayer();
}
}
更新2
javax.media.NotRealizedError: Cannot get visual component on an unrealized playe
r
at com.sun.media.BasicPlayer.getVisualComponent(BasicPlayer.java:491)
at com.sun.media.MediaPlayer.getVisualComponent(MediaPlayer.java:48)
at mediaPlayer.<init>(mediaPlayer.java:29)
at mediaPlayer.main(mediaPlayer.java:38)
Exception in thread "main" javax.media.NotRealizedError: Cannot get visual compo
nent on an unrealized player
at com.sun.media.BasicPlayer.getVisualComponent(BasicPlayer.java:491)
at com.sun.media.MediaPlayer.getVisualComponent(MediaPlayer.java:48)
at mediaPlayer.<init>(mediaPlayer.java:29)
at mediaPlayer.main(mediaPlayer.java:38)
请帮助我播放此视频,删除此nullpointer exception
。非常感谢所有为我的问题付出努力的人:)。
将您的代码更改为这个,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.media.*;
import java.net.URL;
import java.io.*;
import java.net.MalformedURLException;
public class mediaPlayer extends JFrame
{
public mediaPlayer()
{
JFrame f = new JFrame("Video Demo");
f.setLayout(new BorderLayout());
f.setSize(500,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//file you want to play
try{
String name = "file:///"+new File("output.mp4").getAbsolutePath();
URL mediaURL = new URL(name);
Player mediaPlayer = Manager.createRealizedPlayer(mediaURL);
//get components for video and playback controls
Component video = mediaPlayer.getVisualComponent();
Component controls = mediaPlayer.getControlPanelComponent();
f.add(video,BorderLayout.CENTER);
f.add(controls,BorderLayout.SOUTH);
} catch (Exception e) { e.printStackTrace(); }
f.setVisible(true);
}
public static void main(String[] args){
new mediaPlayer();
}
}
感谢Andrew Thompson。
Player是一个接口,因此您无法创建它的对象。相反,使用Manager.createPlayer(源代码)方法来进行工作。这是链接,链接
如果您查看JMF 2.1.1-支持的格式页面,您会发现明显没有提到MP4。从理论上讲,修复问题就像为MP4文件获取服务提供程序接口并将其添加到运行时类路径一样"简单"。
底线是,如果这个项目是为了播放一组有限的格式,这些格式要么是内置的,要么是我们可以提供SPI的,那应该没问题。OTOH JMF不适合作为"通用播放器"。它太旧了&支持的格式太少。