StateHelper帮助JMF中Player和Processor进行状态过度,,MF中Player和Pr
分享于 点击 39741 次 点评:132
StateHelper帮助JMF中Player和Processor进行状态过度,,MF中Player和Pr
MF中Player和Processor的configure(),realize()等方法是异步的,会马上返回;而用StateHelper调用这些方法会阻塞,直到状态过度好了或者timeout了才返回.代码来自 JMF Media Framework API Guide
import javax.media.*;public class StateHelper implements ControllerListener{ 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 e) { // TODO: handle exception } if(System.currentTimeMillis() - startTime > timeOutMillis) break; } } return configured; } public boolean prefetch(int timeOutMillis) { long startTime = System.currentTimeMillis(); synchronized (this) { player.prefetch(); while(!prefetched && !failed) { try { wait(timeOutMillis); } catch (InterruptedException e) { // TODO: handle exception } if(System.currentTimeMillis() - startTime > timeOutMillis) break; } } return prefetched && !failed; } public boolean realize(int timeOutMillis) { long startTime = System.currentTimeMillis(); synchronized (this) { player.realize(); while(!realized && !failed) { try { wait(timeOutMillis); } catch (InterruptedException e) { // TODO: handle exception } if(System.currentTimeMillis() - startTime > timeOutMillis) break; } } return realized; } public boolean playToEndOfMedia(int timeOutMillis) { long startTime = System.currentTimeMillis(); eom = false; synchronized (this) { player.start(); while(!eom && !failed) { try { wait(timeOutMillis); } catch (InterruptedException e) { // TODO: handle exception } if(System.currentTimeMillis() - startTime > timeOutMillis) break; } } return eom && !failed; } public void close() { synchronized (this) { player.close(); while(!closed) { try { wait(100); } catch (InterruptedException e) { // TODO: handle exception } } } player.removeControllerListener(this); } @Override public synchronized void controllerUpdate(ControllerEvent e) { if(e instanceof RealizeCompleteEvent) { realized = true; } else if(e instanceof ConfigureCompleteEvent) { configured = true; } else if(e instanceof PrefetchCompleteEvent) { prefetched = true; } else if(e instanceof EndOfMediaEvent) { eom = true; } else if(e instanceof ControllerErrorEvent) { failed = true; } else if(e instanceof ControllerClosedEvent) { closed = true; } else { return; } notifyAll(); } boolean configured = false; boolean realized = false; boolean prefetched = false; boolean closed = false; boolean failed = false; boolean eom = false; Player player = null;}//该片段来自于http://byrx.net
用户点评