JMF中的StateHelper类

import javax.media.*;

/**
 * StateHelper是处理Player或Processor的同步状态转换的一个很有用的类。
 */
public class StateHelper implements javax.media.ControllerListener {
    Player player = null;
    boolean configured = false;
    boolean realized = false;
    boolean prefetched = false;
    boolean endOfMeida = false;
    boolean failed = false;
    boolean closed = false;
   
    public StateHelper(Player p) {
 player = p;
 p.addControllerListener(this);
    }

    public boolean configure() {
 return configure(Integer.MAX_VALUE);
    }

    /**
     * 配置这个player
     * 这个方法将阻塞直到配置完成或者配置失败任意一种情况发生才返回。
     */
    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() {
 return realize(Integer.MAX_VALUE);
    }
    /**
     * 实现这个player
     * 这个方法将阻塞直到实现完成或者实现失败任意一种情况发生才返回。
     */
    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;
    }

    /**
     * 预取这个player
     * 这个方法将阻塞直到预取完成或者预取失败任意一种情况发生才返回。
     */
    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;
    }

    /**
     * 开始播放这个player,直到媒体的结尾。
     * 这个方法将阻塞直到完成播放或在某些点回放失败任一种情况发生才返回。
     */
    public boolean playToEndOfMedia(int timeOutMillis) {
 long startTime = System.currentTimeMillis();
 endOfMeida = false;
 synchronized (this) {
     player.start();
     while (!endOfMeida && !failed) {
  try {
      wait(timeOutMillis);
  } catch (InterruptedException ie) {
  }
  if (System.currentTimeMillis() - startTime > timeOutMillis)
      break;
     }
 }
 return endOfMeida && !failed;
    }

    /**
     * 关闭这个player
     */
    public void close() {
 synchronized (this) {
     player.close();
     while (!closed) {
  try {
      wait(100);
  } catch (InterruptedException ie) {
  }
     }
 }
 player.removeControllerListener(this);
    }
    /**
     * 同步监听player状态转变事件
     */
    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) {
     endOfMeida = true;
 } else if (ce instanceof ControllerErrorEvent) {
     failed = true;
 } else if (ce instanceof ControllerClosedEvent) {
     closed = true;
 } else {
     return;
 }
 notifyAll();
    }
}

你可能感兴趣的:(类,职场,JMF,休闲,StateHelper)