Help with "media player in JMF"
Hello,
I'm creating a media player in JMF. I have a borderlayout that has a menu bar at the top, the video file that is loaded in the center, and some buttons on the south. I can open the video and play it fine, but when I try to load a new video from the menu bar, the menu itens don't show up. Any ideas why?
Here's the code:
import java.io.*;
import java.awt.*;
import javax.media.*;
import javax.swing.*;
import java.awt.event.*;
public class VideoPlay extends JFrame implements ActionListener
{
//botoes
private JButton comeca;
private JButton parar;
private JButton sair;
private JButton abrir;
private JButton pausar;
private JButton proximo;
private JButton anterior;
//paineis
private JPanel ecran;
private Player realTo;
//MENUS
private JMenuBar menu = new JMenuBar();
private JMenu file = new JMenu("Ficheiro");
private JMenu playlist = new JMenu("Playlist");
private JMenu control = new JMenu("Controlo");
private JMenu comentar = new JMenu("Comentar");
private JMenu sobre = new JMenu("Sobre");
private JMenu help = new JMenu("Ajuda");
//ITENS MENUS
private JMenuItem openFile;
private JMenuItem openURL;
private JMenuItem close;
private JMenuItem exit;
private JMenuItem play;
private JMenuItem pause;
private JMenuItem stop;
private JMenuItem info;
private JMenuItem addList;
private JMenuItem subList;
private JMenuItem ajuda;
private JMenuItem cficheiro;
public VideoPlay ()
{
init();
}
public void init ()
{
//tratar eventos da janela
addWindowListener (new WindowAdapter ()
{
public void windowClosing (WindowEvent we)
{
dispose ();
System.exit (0);
}
});
setSize (600,600);
getContentPane ().setLayout (new BorderLayout ());
ecran = new JPanel ( new BorderLayout ());
//declara玢o dos botoes
abrir = new JButton ("open");
comeca = new JButton ("start");
parar = new JButton ("stop");
sair = new JButton ("close");
pausar = new JButton ("pause");
anterior = new JButton ("previous");
proximo = new JButton ("next");
/*-adicionar items aos menus-*/
//itens do menu file
openFile = new JMenuItem("Abrir Ficheiro");
openURL = new JMenuItem("Abrir URL");
close = new JMenuItem("Fechar");
exit = new JMenuItem("Sair");
//itens do menu playlist
addList = new JMenuItem("Adicionar...");
subList = new JMenuItem("Remover...");
//itens do menu control
play = new JMenuItem("Play");
pause = new JMenuItem("Pause");
stop = new JMenuItem("Stop");
//item do menu Sobre
info = new JMenuItem("Sobre Java Player...");
//item do menu Help
ajuda = new JMenuItem("Como utilizar o Player...");
//itens do menu comentar
cficheiro = new JMenuItem("Comentar Ficheiro");
/*--fim-*/
/*adicionar elementos ao menu respectivo-*/
//adicionar os itens ao menu file
file.add(openFile);
file.add(openURL);
file.add(close);
file.addSeparator();
file.add(exit);
//adicionar os itens ao menu Playlist
playlist.add(addList);
playlist.add(subList);
//adicionar item ao menu sobre
sobre.add(info);
//adicionar os itens ao menu help
help.add(ajuda);
//adicionar o item comentar ao menu comentar
comentar.add(cficheiro);
//adicionar os itens ao menu control
control.add(play);
control.add(pause);
control.add(stop);
control.setEnabled(false);
/*-fim*/
//colocar o menu comentar invisivel enquanto n鉶 existe nada a reproduzir
comentar.setEnabled(false);
//adicionar os menus
menu.add(file);
menu.add(playlist);
menu.add(control);
menu.add(comentar);
menu.add(sobre);
menu.add(help);
menu.setBorderPainted(true);
//atribuir eventos aos itens
openFile.addActionListener(this);
openURL.addActionListener(this);
close.addActionListener(this);
close.setEnabled(false);
exit.addActionListener(this);
play.addActionListener(this);
pause.addActionListener(this);
stop.addActionListener(this);
info.addActionListener(this);
ajuda.addActionListener(this);
/*fim--*/
/*-botoes--*/
//adicionar eventos aos botoes
abrir.addActionListener (this);
comeca.addActionListener (this);
pausar.addActionListener (this);
anterior.addActionListener (this);
proximo.addActionListener (this);
parar.addActionListener (this);
sair.addActionListener (this);
JPanel botoes = new JPanel ();
botoes.setLayout (new FlowLayout ());
botoes.add (abrir);
botoes.add (comeca);
botoes.add (parar);
botoes.add (pausar);
botoes.add (anterior);
botoes.add (proximo);
botoes.add (sair);
/*-fim-*/
getContentPane().add (menu, BorderLayout.NORTH);
getContentPane ().add (botoes, BorderLayout.SOUTH);
/*try
{
realTo = Manager.createRealizedPlayer (
new MediaLocator (new File ("puppet.mpeg").toURL ()));
if (realTo.getVisualComponent () != null)
{
ecran.add (realTo.getVisualComponent (), BorderLayout.CENTER);
}
ecran.add (realTo.getControlPanelComponent (), BorderLayout.SOUTH);
getContentPane().add (ecran, BorderLayout.CENTER);
}
catch (Exception e)
{
e.printStackTrace ();
}*/
}
public void actionPerformed (ActionEvent ae)
{
//para escolher outros videos
if (ae.getSource()==abrir)
{
FileDialog file = new FileDialog (this, "Abrir Ficheiro...", FileDialog.LOAD);
file.show ();
String fileName = file.getDirectory () + file.getFile ();
System.out.println (fileName);
if (realTo != null)
{
realTo.close ();
}
ecran.removeAll();
try
{
realTo = Manager.createRealizedPlayer (
new MediaLocator (new File (fileName).toURL ()));
if (realTo.getVisualComponent () != null)
{
ecran.add (realTo.getVisualComponent (), BorderLayout.CENTER);
}
ecran.add (realTo.getControlPanelComponent (), BorderLayout.SOUTH);
getContentPane().add (ecran, BorderLayout.CENTER);
}
catch (Exception e)
{
e.printStackTrace ();
}
setVisible (true);
}
if (ae.getSource()==comeca)
{
if (realTo != null)
{
realTo.start();
}
}
if (ae.getSource()==parar)
{
if (realTo != null)
{
realTo.stop();
realTo.setMediaTime(new Time(0));
}
}
if (ae.getSource()==pausar)
{
if (realTo != null)
{
realTo.stop();
}
}
if (ae.getSource()==sair)
{
if (realTo != null)
{
realTo.close();
System.err.println ("Program has been terminated...");
System.exit (0);
}
}
/*--tratar eventos sobre os itens do menu*/
if (ae.getSource()==openFile)
{
FileDialog file = new FileDialog (this, "Abrir Ficheiro...",
FileDialog.LOAD);
file.show ();
String fileName = file.getDirectory () + file.getFile ();
System.out.println (fileName);
if (realTo != null)
{
realTo.close ();
}
ecran.removeAll();
try
{
realTo = Manager.createRealizedPlayer (
new MediaLocator (new File (fileName).toURL ()));
if (realTo.getVisualComponent () != null)
{
ecran.add (realTo.getVisualComponent (), BorderLayout.CENTER);
}
ecran.add (realTo.getControlPanelComponent (), BorderLayout.SOUTH);
getContentPane().add (ecran, BorderLayout.CENTER);
}
catch (Exception e)
{
e.printStackTrace ();
}
setVisible (true);
}
if (ae.getSource()==play)
{
if (realTo != null)
{
realTo.start();
}
}
if (ae.getSource()==stop)
{
if (realTo != null)
{
realTo.stop();
realTo.setMediaTime(new Time(0));
}
}
if (ae.getSource()==pause)
{
if (realTo != null)
{
realTo.stop();
}
}
if (ae.getSource()==close)
{
if (realTo != null)
{
realTo.close();
System.err.println ("Program has been terminated...");
System.exit (0);
}
}
//evento sobre o item info
if (ae.getSource() == info) {
AboutDialog ad = new AboutDialog(this, "Sobre o Java Player");
ad.setMessage("Realizado por David Ribeiro e Ricardo Santos.");
ad.show();
}
//evento sobre o item about
if (ae.getSource() == ajuda) {
AboutDialog ad = new AboutDialog(this, "Sobre o Java Player");
ad.setMessage("Realizado por David Ribeiro e Ricardo Santos.");
ad.show();
}
/*-fim*/
}
public static void main (String [] args)
{
VideoPlay jan = new VideoPlay ();
jan.show ();
}
}
/*--*/
class AboutDialog extends JDialog implements ActionListener {
private static int LOC_X = 50;
private static int LOC_Y = 50;
private static int WIDTH = 250;
private static int HEIGHT = 100;
private JPanel panel;
private JPanel okPanel;
private JLabel msgLabel;
private JButton ok;
public AboutDialog(Frame owner, String title) {
super(owner, title, true);
this.draw();
}
public void setMessage(String msg) {
msgLabel.setText(msg);
}
private void draw() {
this.setLocation(LOC_X, LOC_Y);
this.setSize(WIDTH, HEIGHT);
panel = new JPanel(new BorderLayout());
okPanel = new JPanel(new FlowLayout());
msgLabel = new JLabel();
msgLabel.setHorizontalAlignment(JLabel.CENTER);
ok = new JButton("OK");
ok.addActionListener(this);
panel.add(msgLabel, BorderLayout.CENTER);
okPanel.add(ok);
panel.add(okPanel, BorderLayout.SOUTH);
this.getContentPane().add(panel);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == ok) {
this.dispose();
}
}
}
class URLDialog extends JDialog implements ActionListener {
private static int LOC_X = 50;
private static int LOC_Y = 50;
private static int WIDTH = 250;
private static int HEIGHT = 100;
private String url = null;
private JPanel panel;
private JPanel urlPanel;
private JPanel buttonPanel;
private JLabel label;
private JTextField urlField;
private JButton ok;
private JButton cancel;
public URLDialog(Frame owner, String title) {
super(owner, title, true);
this.draw();
}
private void draw() {
this.setLocation(LOC_X, LOC_Y);
this.setSize(WIDTH, HEIGHT);
panel = new JPanel(new BorderLayout());
urlPanel = new JPanel(new FlowLayout());
buttonPanel = new JPanel(new FlowLayout());
label = new JLabel("URL");
urlField = new JTextField(25);
ok = new JButton("OK");
ok.addActionListener(this);
cancel = new JButton("Cancel");
cancel.addActionListener(this);
urlPanel.add(label);
urlPanel.add(urlField);
panel.add(urlPanel, BorderLayout.CENTER);
buttonPanel.add(ok);
buttonPanel.add(cancel);
panel.add(buttonPanel, BorderLayout.SOUTH);
this.getContentPane().add(panel);
}
public String getURL() {
return url;
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == ok) {
if (urlField.getText().length() == 0) {
url = null;
}
else {
url = urlField.getText();
}
this.dispose();
}
else if (ae.getSource() == cancel) {
url = null;
this.dispose();
}
}
}

