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:).