1st Class :
jb4=new JButton("Select the File");
jb4.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
final JFrame JF=new JFrame("Video Player");
JFileChooser fc=new JFileChooser();
fc.showOpenDialog(null);
try {
mediaURL = fc.getSelectedFile().toURI();
} catch (MalformedURLException ex) {
System.out.println(ex.getMessage());
}
final MediaPlayer mp= new MediaPlayer(mediaURL) ;
panel4.add(mp);
}
});
tabbedPane.addTab("Video Player", createImageIcon("images/VideoPlayer.png"), panel4,"This tab is for Video Player");
panel4.add(jb4);
jb=new JButton("Close Video Frame");
jb.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
// WHAT SHOULD I WRITE HERE???
}
});
panel4.add(jb);
2nd Class :
public class MediaPlayer extends JPanel {
JPanel JP=new JPanel();
JButton jb=new JButton("Close");
public MediaPlayer(URL mediauUrl) {
try{
UIManager.setLookAndFeel(new SyntheticaAluOxideLookAndFeel());
}catch (Exception e)
{ e.printStackTrace(); }
setLayout(new GridLayout(1,1));
try {
final Player mediaPlayer=Manager.createRealizedPlayer(new MediaLocator(mediauUrl)); // LINE A
Component video=mediaPlayer.getVisualComponent();
Component control=mediaPlayer.getControlPanelComponent();
if (video!=null) {
add(video, BorderLayout.CENTER);
}
add(control, BorderLayout.SOUTH);
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
In plain simple words :
I was building a Video Player using JMF.
In 1st Class we can see a button "jb4". On clicking this it will create an object of 2nd class[MediaPlayer Class] and the Video Player starts.
Now on clicking the other Button "jb" the video player should stop and close itself.For stopping and Closing Video I needed to access the Player Object(mediaPlayer) - SEE LINE A in 2nd class.
So my problem was that I declared that object locally inside 2nd Class.
I thought there might be some way to access that mediaPlayer variable of 2nd class through 1st class. I mean I thought I could write some code in ActionListner of Button "jb" which will access that mediaPlayer variable. THIS THINKING WAS WRONG.
My problem was resolved when I declared Player Object outside the constructor which is seen in my Answer below.