chatt 0 Newbie Poster

Dear all,

I am a newbie in JMF and just got a problem in running my first JMF java program.

I wanted to create a player and play some video. And I happened to get some code which could run properly. But after my further exploration, it seems that I could not understand some of the part. Could anyone please enlighten there? Thanks in advance.

Questions:

1. when I add in thePlayer.realize(); after thePlayer.start(); in method start(), I get compilation error as such

++++++++++++++++
C:\\test>java MPlayer
javax.media.NotRealizedError: Cannot get gain control on an unrealized player
at com.sun.media.MediaPlayer.getGainControl(MediaPlayer.java:58)
at MPlayer.start(MPlayer.java:215)
at MPlayer.<init>(MPlayer.java:158)
at MPlayer.main(MPlayer.java:252)
Exception in thread "main" javax.media.NotRealizedError: Cannot get
gain control on an unrealized player
at com.sun.media.MediaPlayer.getGainControl(MediaPlayer.java:58)
at MPlayer.start(MPlayer.java:215)
at MPlayer.<init>(MPlayer.java:158)
at MPlayer.main(MPlayer.java:252)
++++++++++++++++++

I understand that the problem is that player is not realized yet. But may I ask what is the importance to realize the player? Meanwhile, when I read about some relavant meterial, I got to know that player has many phases like unrealized, realized, prefetched. May I ask if I need to know each and every phase well before I could realy write my own player using JMF? If it is, could you kindly give me some hint to find relavant meterial regarding that?

2. there are some lines to implement the actionPerformed.

++++++++++++++++
public void actionPerformed(ActionEvent event) {
setVisible(false);
System.exit(0);
}
++++++++++++++++

I know that since the MPlayer class is implementing ActionListener, ControllerListener, so it has to implement actionPerformed. But May I ask what will be the action which will trigger this listener? I have tried playing with it. I still could not figure it out.

3. Regarding the class --> public void controllerUpdate(ControllerEvent event)

I realised that it is called automatically by the system. I wonder if it is the function that its super class has or else how come it acts like such.


That is all for my Questions. Your help will be much appreciated. BTW, code is attached.

Code attached:

++++++++++++++++
import javax.media.Controller;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Player;
import javax.media.Manager;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
import javax.media.GainControl;


import java.io.IOException;
import javax.media.NoPlayerException;
import javax.media.CannotRealizeException;
import java.net.URL;
import java.io.File;
import java.net.MalformedURLException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JFileChooser;
import java.awt.Container;
import java.awt.Component;
import javax.swing.BoxLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

public class MPlayer
extends JFrame
implements ActionListener, ControllerListener {

private Player thePlayer;
private File theFile;
private JLabel currentTimeLabel, mediaNameLabel;
private int iTime;
private String theTime;
private boolean stop = false;
private Font font;
private Color textColor;
private Dimension size;
private JMenuBar MenuBar;
private JMenu FileMenu;
private JMenuItem OpenItem, ExitItem;
private JFileChooser mediaFileChooser;

private JPanel timePanel, menuPanel;

private Container pane = getContentPane();

public MPlayer()
{

font = new Font("Times", Font.BOLD, 18);

textColor = new Color(100, 0, 0);

theFile = new File("C:\\");

//theFile = new File("\\..\\..\\..\\..\\Documents and Settings\\Mark Brown\\My Documents\\My Music\\cornelius_-_count_five_or_six.mp3");

//theFile = new File("Z:\\Temp2\\creatures-demo.mpeg");

mediaFileChooser = new JFileChooser();

JMenuBar MenuBar = new JMenuBar();

JMenu FileMenu = new JMenu();

JMenuItem OpenItem = new JMenuItem();

JMenuItem ExitItem = new JMenuItem();

FileMenu.setText("File");

OpenItem.setText("Open");

ExitItem.setText("Exit");

FileMenu.add(OpenItem);

FileMenu.add(ExitItem);

MenuBar.add(FileMenu);

menuPanel = new JPanel();

menuPanel.setLayout(new BorderLayout());

menuPanel.add(MenuBar, BorderLayout.NORTH);

menuPanel.setPreferredSize(new Dimension(700, 25));

menuPanel.setMaximumSize(new Dimension(700, 25));

timePanel = new JPanel();

size = new Dimension(700, 40);

timePanel.setPreferredSize(size);

timePanel.setMaximumSize(size);

timePanel.setLayout(new FlowLayout(0, 15, 10));

currentTimeLabel = new JLabel(theTime);

currentTimeLabel.setFont(font);

currentTimeLabel.setForeground(textColor);

pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

pane.add(menuPanel);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//OpenItem.addActionListener(new ActionListener());

// loop forever updating gui

//Removed lots from here

/*
public void actionPerformed(ActionEvent event)
{

int retval = mediaFileChooser.showOpenDialog(null);

if (retval == JFileChooser.APPROVE_OPTION)
{

theFile = mediaFileChooser.getSelectedFile();
//make sure this method returns.
start();

}

}

});
*/
start();
pack();
setVisible(true);

}

public void start()
{
try
{

//thePlayer = Manager.createPlayer(theFile.toURL());


//zj 0920
//System.out.println("zj @ start theFile.toURL() --> " + theFile.toURL());
//System.out.println("zj @ start Time t --> t.getSeconds() " + t.getSeconds());
//System.out.println("zj @ start theFile.toURL() --> " + theFile.toURL());
//System.exit(1);

//end
thePlayer = Manager.createPlayer(new URL("file:/C:/Documents and Settings/isc10466/My Documents/Video 1.avi"));


//zj 0920


//end
thePlayer.addControllerListener(this);
iTime = (int) thePlayer.getMediaTime().getSeconds();

theTime = String.valueOf(iTime + " seconds @ zj");

currentTimeLabel.setText(theTime);

mediaNameLabel = new JLabel(" " + theFile.getName());

mediaNameLabel.setFont(font);

mediaNameLabel.setForeground(textColor);

timePanel.add(currentTimeLabel);

timePanel.add(mediaNameLabel);

pane.add(timePanel);

pack();

setVisible(true);

thePlayer.start();

thePlayer.realize();
//thePlayer.setStopTime(t);
GainControl gc = thePlayer.getGainControl();
//getDB()
//System.out.println("zj @ start gaincontrol. getDB() --> " + gc.getDB());
//System.exit(1);

}
catch (MalformedURLException m)
{
dialogueBox(m.toString());
}
catch (IOException i)
{
dialogueBox(i.toString());
} catch (NoPlayerException n) {
dialogueBox(n.toString());
}

}

public void stop() {
stop = true;
thePlayer.stop();
thePlayer.close();
}

public void actionPerformed(ActionEvent event) {
setVisible(false);
System.exit(0);
}

public void dialogueBox(String text)
{
JOptionPane.showMessageDialog(null,text," message",JOptionPane.INFORMATION_MESSAGE);
}

public static void main(String[] argv) {

MPlayer p = new MPlayer();

}
/* (non-Javadoc)
* @see javax.media.ControllerListener#controllerUpdate(javax.media.ControllerEvent)
*/
/**
* This controllerUpdate function must be defined in order to
* implement a ControllerListener interface. This
* function will be called whenever there is a media event
*/
public void controllerUpdate(ControllerEvent event) {
Object controlComponent = null;
// If we're getting messages from a dead player,
// just leave
if (thePlayer == null)
return;

// When the player is Realized, get the visual
// and control components and add them to the Applet
if (event instanceof RealizeCompleteEvent) {
//if (progressBar != null) {
//panel.remove(progressBar);
//progressBar = null;
System.out.println("Realized");
if (thePlayer.getVisualComponent() != null) {

pane.add(thePlayer.getVisualComponent());
pane.add(thePlayer.getControlPanelComponent());
}
GainControl gc = thePlayer.getGainControl();
//getDB()
System.out.println("zj @ controllerUpdate gaincontrol. getDB() --> " + gc.getDB());
//
Time t = new Time((double)10);

//gc.setDB(-10.0f);
gc.setLevel(0.5f);
thePlayer.setStopTime(t);
//System.exit(1);
//stop();
System.out.println("zj @ controllerUpdate gaincontrol. getDB() --> " + gc.getDB());

updateGui();


}

}
/**
*
*/
private void updateGui() {
// TODO Auto-generated method stub
double dt = 0;
while (!stop) {
try {
Thread.sleep(500);

dt = thePlayer.getMediaTime().getSeconds();

theTime = String.valueOf((int) dt + " secs");

currentTimeLabel.setText(theTime);
System.out.println(dt);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}
++++++++++++++++