CONTENTS | PREV | NEXT | INDEX JMF 2.0 API Guide



B

StateHelper

StateHelper is a helper class that implements the ControllerListener interface and can be used to manage the state of a Processor. This helper class is used in examples 5-4, 5-5, and 5-6 inCapturing Time-Based Media with JMF.
Example B-1: StateHelper (1 of 3)
 import javax.media.*;
 
 
 public class StateHelper implements javax.media.ControllerListener {
 
     Player player = null;
     boolean configured = false;
     boolean realized = false;
     boolean prefetched = false;
     boolean eom = false;
     boolean failed = false;
     boolean closed = false;
     
     public StateHelper(Player p) {
 	 player = p;
 	 p.addControllerListener(this);
     }
 
     public boolean configure(int timeOutMillis) {
 	 long startTime = System.currentTimeMillis();
 	 synchronized (this) {
 	     if (player instanceof Processor)
 	 	 ((Processor)player).configure();
 	     else
 	 	 return false;
 	     while (!configured && !failed) {
 	 	 try {
 	 	     wait(timeOutMillis);
 	 	 } catch (InterruptedException ie) {
 	 	 }
 	 	 if (System.currentTimeMillis() - startTime > timeOutMillis)
 	 	     break;
 	     }
 	 }
 	 return configured;
     }
     
     public boolean realize(int timeOutMillis) {
 	 long startTime = System.currentTimeMillis();
 	 synchronized (this) {
 	     player.realize();
 	     while (!realized && !failed) {
 	 	 try {
 	 	     wait(timeOutMillis);
 	 	 } catch (InterruptedException ie) {
 	 	 }
 	 	 if (System.currentTimeMillis() - startTime > timeOutMillis)
 	 	     break;
 	     }
 	 }
 	 return realized;
     }
 
     public boolean prefetch(int timeOutMillis) {
 	 long startTime = System.currentTimeMillis();
 	 synchronized (this) {
 	     player.prefetch();
 	     while (!prefetched && !failed) {
 	 	 try {
 	 	     wait(timeOutMillis);
 	 	 } catch (InterruptedException ie) {
 	 	 }
 	 	 if (System.currentTimeMillis() - startTime > timeOutMillis)
 	 	     break;
 	     }
 	 }
 	 return prefetched && !failed;
     }
     public boolean playToEndOfMedia(int timeOutMillis) {
 	 long startTime = System.currentTimeMillis();
 	 eom = false;
 	 synchronized (this) {
 	     player.start();
 	     while (!eom && !failed) {
 	 	 try {
 	 	     wait(timeOutMillis);
 	 	 } catch (InterruptedException ie) {
 	 	 }
 	 	 if (System.currentTimeMillis() - startTime > timeOutMillis)
 	 	     break;
 	     }
 	 }
 	 return eom && !failed;
     }
 
     public void close() {
 	 synchronized (this) {
 	     player.close();
 	     while (!closed) {
 	 	 try {
 	 	     wait(100);
 	 	 } catch (InterruptedException ie) {
 	 	 }
 	     }
 	 }
 	 player.removeControllerListener(this);
     }
 
     public synchronized void controllerUpdate(ControllerEvent ce) {
 	 if (ce instanceof RealizeCompleteEvent) {
 	     realized = true;
 	 } else if (ce instanceof ConfigureCompleteEvent) {
 	     configured = true;
 	 } else if (ce instanceof PrefetchCompleteEvent) {
 	     prefetched = true;
 	 } else if (ce instanceof EndOfMediaEvent) {
 	     eom = true;
 	 } else if (ce instanceof ControllerErrorEvent) {
 	     failed = true;
 	 } else if (ce instanceof ControllerClosedEvent) {
 	     closed = true;
 	 } else {
 	     return;
 	 }
 	 notifyAll();
     }
 }



CONTENTS | PREV | NEXT | INDEX

Copyright © 1998-1999 Sun Microsystems, Inc. All Rights Reserved.