Home arrow static arrow Java Programming [Archive] - Managing Threads
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Managing Threads
This topic has 3 replies on 1 page.

Posts:35
Registered: 11/11/03
Managing Threads  
Jul 19, 2004 12:14 PM



 
Hi there,

I'm trying to implement a media player that will run as a separate thread.

I have a problem tracking the state of the player. To track the player's state i have i variable called isActive which should be true when the player is playing, and false otherwise.

The problem is in the run() method. In this method I set isActive to true and call the player.start() method. Once the player finished playing, I need to set isActive to false (before exiting the run() method). The problem is that after calling player.start() the execution proceeds on to the next line and the value isActive is set to false eventhough the player is still playing.

Can anyone suggest a solution to this problem? Many thanks in advance for any help.

Arman

public class MediaPlayerThread implements MediaProcessor, ControllerListener {        /** javax.media.Player object */    private Player player;        /** Indicates whether the player is currently playing  */    private boolean isActive = false;        /** Constructor */    public MediaPlayerThread(File mediaFile) {		try {		    MediaLocator locator = new MediaLocator(mediaFile.toURL());			player = Manager.createRealizedPlayer(locator);			player.addControllerListener(this);		}		catch (Exception e) {		}    }        /** Start playing media */    public void startProcessing() {        new Runnable() {            public void run() {                if(player != null || isActive != true) {                     // Player starting to play                    isActive = true;                    player.start();                                        // Player finished playing                    isActive = false;                }            }        };    }        /** Stop playing media */    public void stopProcessing() {        player.stop();        isActive = false;    }        /** Tests whether player is currently playing */    public boolean isActive() {        return isActive;    }        /** ControllerListener method */	public synchronized void controllerUpdate(ControllerEvent event) {		if (event instanceof EndOfMediaEvent) {		    processEvent(event);		} else if (event instanceof StopByRequestEvent) {		    processEvent(event);		}	}		/* Private */	private void processEvent(ControllerEvent event) {		Controller controller = (Controller)event.getSource();		controller.stop();		controller.close();		isActive = false;	}	}
 

Posts:18,384
Registered: 21.03.00
Re: Managing Threads  
Jul 19, 2004 12:54 PM (reply 1 of 3)



 
Hi,

I don't have the documentation for the Player, but check if player.start() returns the thread that is playing, or if you can get it in some other way. Then class join on it.

Thread t = player.start();   //Check the javadoc and see if this workst.join();


/Kaj
 

Posts:3,055
Registered: 18/06/98
Re: Managing Threads  
Jul 19, 2004 12:57 PM (reply 2 of 3)



 
Suggestion (I've never used javax.media before, just reading the javadocs)

1) Remove the "isActive = false" from the code after player.start().
                    player.start();                    // Player finished playing                                        isActive = false;


2) Put the "isActive = false" inside the controllerUpdate method in the events that indicate that the player finished playing.
    /** ControllerListener method */	    public synchronized void controllerUpdate(ControllerEvent event) {        if (event instanceof EndOfMediaEvent) {		                processEvent(event);		            isActive = false;        } else if (event instanceof StopByRequestEvent) {		                processEvent(event);		            isActive = false;        }	    }	
 

Posts:35
Registered: 11/11/03
Re: Managing Threads  
Jul 19, 2004 3:13 PM (reply 3 of 3)



 
Many thanks for the suggestions. I've never worked with threads so not sure what would be the best way to organise this class.

I got rid off
isActive = false
in the playback method. What about the new version of the class below? Does it make "more sense" than the previous one?

Would appreciate your comments.

Arman

public class MessagePlayer extends Thread implements MediaProcessor, ControllerListener {        /** javax.media.Player object */    private Player player;        /** Indicates whether the player is currently playing  */    private boolean isActive = false;    	/**	 * Class constructor taking a <code>java.io.File</code>	 * as its argument.	 * 	 * @param mediaFile to play.	 * @throws MessagePlayerException if an error occurs	 * constructing the player.	 */    public MessagePlayer(File mediaFile) throws MessagePlayerException {		try {		    MediaLocator locator = new MediaLocator(mediaFile.toURL());		    player = Manager.createRealizedPlayer(locator);		    player.addControllerListener(this);		}		catch (Exception e) {			throw new MessagePlayerException				("Error constructing MediaPlayer: \n\n" + e);		}    }        /** Start playing media */    public void startProcessing() {        new Thread(new Runnable() {            public void run() {                if(player != null || isActive != true) {                    isActive = true;                                player.start();                }            }        }).start();    }        /** Stop playing media */    public void stopProcessing() {        player.stop();        isActive = false;    }        /** Close player and free up all resources */    public void close() {        player.stop();        player.deallocate();        player.close();    }        /** Tests whether player is currently playing */    public boolean isActive() {        return isActive;    }        /** ControllerListener method */	public synchronized void controllerUpdate(ControllerEvent event) {		if (event instanceof EndOfMediaEvent) {		    processEvent(event);		} else if (event instanceof StopByRequestEvent) {		    processEvent(event);		}	}		/* Private */	private void processEvent(ControllerEvent event) {		Controller controller = (Controller)event.getSource();		controller.stop();		controller.setMediaTime(new Time(0));		controller.deallocate();		isActive = false;	}}
 
This topic has 3 replies on 1 page.