I am trying to play a video using jmf. After working hard for hours literally removing all errors and exceptions, Here I am getting a null pointer exception. Here I figured out after looking on NullPointerException, this should be due to incorrect declaration of Player mediaPlayer where it is not initialised to anything.
Another problem with directly initialising it to what values I am giving it later is, I have to catch the exceptions too, then it says player might not be declared.
How do I declare mediaURL
and Player
so that this nullpointerexception
is removed and I can play this video.
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();
}
}
Update2
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)
Please help me in playing this video, removing this nullpointer exception
. Thanks a lot to everyone who puts any effort on my problem:).
If you check the JMF 2.1.1 - Supported Formats page, you'll notice a distinct lack of mention for MP4. Theoretically, fixing the problem as as 'simple' as obtaining a Service Provider Interface for MP4 files, and adding it to the run-time class-path.
The bottom line is that if this project is for playing a limited group of formats that are either inbuilt, or for which we can provide an SPI, it should be fine. OTOH JMF is not suitable as a 'general purpose player'. It is too old & supports too few formats.
Player is an interface so you cannot make its object. Instead use Manager.createPlayer(source) method to get your working. Here is the link , link
Change your code to this,
Credits-Andrew Thompson.