Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using thread methods to control video player

Status
Not open for further replies.

kb22

Programmer
Nov 22, 2006
2
0
0
GB
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();
}
}
}




}





 
One observation. The tfPlayer class is started in a new thread, but in its run() method, it displays the frame, but then exits immediately. The thread ends when run() ends. If the thread has finished, you'll not be able to manipulate it with notify and wait etc.

Another thought ... a graphical thing like a video 'player' will need to be running in the EventDispatch thread (otherwise all sorts of interesting things could happen visually w/regards to screen refresh and stuff). Pausing it by halting its thread would then mean pausing the EventDispatch thread, which is *bad*. It would lock your application completely.

Oh, and I had the devil of a time scrolling up and down your code trying to see where each class finished and the next one started. Please use the 'code' tags provided by this forum to make your code easier to read, making sure your indentation is reader-friendly.

Tim
 
Thank you for your advice I will go back to the drawing board. I am going to look at the Java Threads book.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top