JMF Example
  1 package jmfexample;
  2 
  3 import javax.media.*;
  4 
  5 
  6 public class camStateHelper implements javax.media.ControllerListener {
  7     
  8     Player player = null;
  9     boolean configured = false;
 10     boolean realized = false;
 11     boolean prefetched = false;
 12     boolean eom = false;
 13     boolean failed = false;
 14     boolean closed = false;
 15     
 16     public camStateHelper(Player p) {
 17         player = p;
 18         p.addControllerListener(this);
 19     }
 20     
 21     public boolean configure(int timeOutMillis) {
 22         
 23         long startTime = System.currentTimeMillis();
 24         synchronized (this) {
 25             if (player instanceof Processor)
 26                 ((Processor)player).configure();
 27             else
 28                 return false;
 29             while (!configured && !failed) {
 30                 try {
 31                     wait(timeOutMillis);
 32                 } catch (InterruptedException ie) {
 33                 }
 34                 if (System.currentTimeMillis() - startTime > timeOutMillis)
 35                     break;
 36             }
 37         }
 38         return configured;
 39     }
 40     
 41     public boolean realize(int timeOutMillis) {
 42         long startTime = System.currentTimeMillis();
 43         synchronized (this) {
 44             player.realize();
 45             while (!realized && !failed) {
 46                 try {
 47                     wait(timeOutMillis);
 48                 }catch (InterruptedException ie) {
 49                 }
 50                 if (System.currentTimeMillis() - startTime > timeOutMillis)
 51                     break;
 52             }
 53         }
 54         return realized;
 55     }
 56     
 57     public boolean prefetch(int timeOutMillis) {
 58         long startTime = System.currentTimeMillis();
 59         synchronized (this) {
 60             player.prefetch();
 61             while (!prefetched && !failed) {
 62                 try {
 63                     wait(timeOutMillis);
 64                 }catch (InterruptedException ie) {
 65                 }
 66                 if (System.currentTimeMillis() - startTime > timeOutMillis)
 67                     break;
 68             }
 69         }
 70         return prefetched && !failed;
 71     }
 72     public boolean playToEndOfMedia(int timeOutMillis) {
 73         long startTime = System.currentTimeMillis();
 74         eom = false;
 75         synchronized (this) {
 76             player.start();
 77             while (!eom && !failed) {
 78                 try {
 79                     wait(timeOutMillis);
 80                 }catch (InterruptedException ie) {
 81                 }
 82                 if (System.currentTimeMillis() - startTime > timeOutMillis)
 83                     break;
 84             }
 85         }
 86         return eom && !failed;
 87     }
 88     
 89     public void close() {
 90         synchronized (this) {
 91             player.close();
 92             while (!closed) {
 93                 try {
 94                     wait(100);
 95                 } catch (InterruptedException ie) {
 96                 }
 97             }
 98         }
 99         player.removeControllerListener(this);
100     }
101     
102     public synchronized void controllerUpdate(ControllerEvent ce) {
103         if (ce instanceof RealizeCompleteEvent) {
104             realized = true;
105         }else if (ce instanceof ConfigureCompleteEvent) {
106             configured = true;
107         } else if (ce instanceof PrefetchCompleteEvent) {
108             prefetched = true;
109         } else if (ce instanceof EndOfMediaEvent) {
110             eom = true;
111         } else if (ce instanceof ControllerErrorEvent) {
112             failed = true;
113         } else if (ce instanceof ControllerClosedEvent) {
114             closed = true;
115         } else {
116             return;
117         }
118         notifyAll();
119     }
120 }
121 
122