JMF中的StateHelper类
2008-03-24 14:49:55
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://gending.blog.51cto.com/326022/67793 |
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(); } } 本文出自 “全丁风” 博客,请务必保留此出处http://gending.blog.51cto.com/326022/67793 本文出自 51CTO.COM技术博客 |


gending
博客统计信息
热门文章
最新评论
友情链接
