I am trying to use the thread methods Wait(),Notify() or NotifyAll() as pause and resume for a video player. I can start the video player as a separate thread but when I try and use pause I get an AWT thread exception. The player is in Thread-2. The application uses the Java Media Framework for the video player functionality.
The code is (viewer.java):
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.media.*;
public class viewer
{
public static void main(String[] args)
{
VideoPlayer vp= new VideoPlayer();
}
}
class VideoPlayer extends JFrame implements ActionListener
{
private tfPlayer aplayer;
private File file;
private Container cmp;
private Thread t;
public VideoPlayer()
{
super ();
addWindowListener(new MyCloser());
setTitle("Video Player");
setSize(200,200);
cmp = getContentPane();
cmp.setLayout(new GridLayout(3,1));
tfPlayer aplayer = new tfPlayer();
Thread t = new Thread(aplayer);
JButton startButton = new JButton("Start");
startButton.addActionListener(this);
cmp.add(startButton);
JButton pauseButton = new JButton("Pause");
pauseButton.addActionListener(this);
cmp.add(pauseButton);
JButton resumeButton = new JButton("Resume");
resumeButton.addActionListener(this);
cmp.add(resumeButton);
setVisible (true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand ().equals ("Start"))
{
start();
}
if (e.getActionCommand ().equals ("Pause"))
{
//wait();
}
if (e.getActionCommand ().equals ("Resume"))
{
//notifyAll();
}
}
public void start()
{
t.start();
}
}
class MyCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
class tfPlayer extends JFrame implements Runnable
{
private Player player;
private File file;
public tfPlayer()
{
JButton openFile = new JButton( "Click to open video to play" );
openFile.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e )
{
openVideo();
producePlayer();
}
}
);
getContentPane().add( openFile );
setSize( 500, 500 );
show();
}
public void run()
{
//player.start();
this.show();
}
private void openVideo()
{
JFileChooser selectfile = new JFileChooser();
selectfile.setFileSelectionMode(
JFileChooser.FILES_ONLY );
int result = selectfile.showOpenDialog( this );
if ( result == JFileChooser.CANCEL_OPTION )
file = null;
else
file = selectfile.getSelectedFile();
}
private void producePlayer()
{
if ( file == null )
return;
removePlayer();
try {
player = Manager.createPlayer( file.toURL() );
player.addControllerListener( new EventHandler() );
player.start();
}
catch (Exception e) {
JOptionPane.showMessageDialog( this, " Wrong file", "Problem",
JOptionPane.ERROR_MESSAGE );
}
}
private void removePlayer()
{
if ( player == null )
return;
player.close();
Component visual = player.getVisualComponent();
Component control = player.getControlPanelComponent();
Container c = getContentPane();
if (visual != null )
c.remove( visual );
if (control != null )
c.remove( control );
}
private class EventHandler implements ControllerListener
{
public void controllerUpdate( ControllerEvent e )
{
if ( e instanceof RealizeCompleteEvent )
{
Container c = getContentPane();
Component visualComponent =
player.getVisualComponent();
if ( visualComponent != null )
c.add( visualComponent, BorderLayout.CENTER );
Component controlsComponent =
player.getControlPanelComponent();
if (controlsComponent != null )
c.add( controlsComponent, BorderLayout.SOUTH );
c.doLayout();
}
}
}
}
The code is (viewer.java):
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.media.*;
public class viewer
{
public static void main(String[] args)
{
VideoPlayer vp= new VideoPlayer();
}
}
class VideoPlayer extends JFrame implements ActionListener
{
private tfPlayer aplayer;
private File file;
private Container cmp;
private Thread t;
public VideoPlayer()
{
super ();
addWindowListener(new MyCloser());
setTitle("Video Player");
setSize(200,200);
cmp = getContentPane();
cmp.setLayout(new GridLayout(3,1));
tfPlayer aplayer = new tfPlayer();
Thread t = new Thread(aplayer);
JButton startButton = new JButton("Start");
startButton.addActionListener(this);
cmp.add(startButton);
JButton pauseButton = new JButton("Pause");
pauseButton.addActionListener(this);
cmp.add(pauseButton);
JButton resumeButton = new JButton("Resume");
resumeButton.addActionListener(this);
cmp.add(resumeButton);
setVisible (true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand ().equals ("Start"))
{
start();
}
if (e.getActionCommand ().equals ("Pause"))
{
//wait();
}
if (e.getActionCommand ().equals ("Resume"))
{
//notifyAll();
}
}
public void start()
{
t.start();
}
}
class MyCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
class tfPlayer extends JFrame implements Runnable
{
private Player player;
private File file;
public tfPlayer()
{
JButton openFile = new JButton( "Click to open video to play" );
openFile.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e )
{
openVideo();
producePlayer();
}
}
);
getContentPane().add( openFile );
setSize( 500, 500 );
show();
}
public void run()
{
//player.start();
this.show();
}
private void openVideo()
{
JFileChooser selectfile = new JFileChooser();
selectfile.setFileSelectionMode(
JFileChooser.FILES_ONLY );
int result = selectfile.showOpenDialog( this );
if ( result == JFileChooser.CANCEL_OPTION )
file = null;
else
file = selectfile.getSelectedFile();
}
private void producePlayer()
{
if ( file == null )
return;
removePlayer();
try {
player = Manager.createPlayer( file.toURL() );
player.addControllerListener( new EventHandler() );
player.start();
}
catch (Exception e) {
JOptionPane.showMessageDialog( this, " Wrong file", "Problem",
JOptionPane.ERROR_MESSAGE );
}
}
private void removePlayer()
{
if ( player == null )
return;
player.close();
Component visual = player.getVisualComponent();
Component control = player.getControlPanelComponent();
Container c = getContentPane();
if (visual != null )
c.remove( visual );
if (control != null )
c.remove( control );
}
private class EventHandler implements ControllerListener
{
public void controllerUpdate( ControllerEvent e )
{
if ( e instanceof RealizeCompleteEvent )
{
Container c = getContentPane();
Component visualComponent =
player.getVisualComponent();
if ( visualComponent != null )
c.add( visualComponent, BorderLayout.CENTER );
Component controlsComponent =
player.getControlPanelComponent();
if (controlsComponent != null )
c.add( controlsComponent, BorderLayout.SOUTH );
c.doLayout();
}
}
}
}