·您当前的位置:首页 > 技术教程 > Flex技术 >

[Flex]as3.0检测当前摄像头状态(占用,没有,正常)三种状态

时间:2013-04-09 20:56CuPlayer.com
在视频程序的编写过程中,我们经常要使用摄像头,在使用摄像头前有必要对摄像头的现有状态做个检测(是否被占用,没安装摄像头,正常)

在视频程序的编写过程中,我们经常要使用摄像头,在使用摄像头前有必要对摄像头的现有状态做个检测:

1.被占用

2.没安装摄像头

3.正常

看下面代码:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" 
  3.     layout="absolute" creationComplete="init()"> 
  4.     <mx:Script> 
  5.         <![CDATA[ 
  6.             import mx.controls.Alert; 
  7.             private var camera:Camera; 
  8.             private var isExistsCamera:Boolean=false; 
  9.             private var isBusyCamera:Boolean=false; 
  10.             private var intervalId:uint; 
  11.             private var times:int; 
  12.             private function init():void 
  13.             { 
  14.                 camera=Camera.getCamera(); 
  15.                if (camera == null) 
  16.                 { 
  17.                    if (Camera.names.length <= 0) 
  18.                     { 
  19.                         this.isExistsCamera=false; 
  20.                         Alert.show("CuPlayer.com提示没安装摄像头"); 
  21.                     } 
  22.                 } 
  23.                 else 
  24.                 { 
  25.                     this.vdpaly.attachCamera(this.camera); 
  26.                     this.isCameraBusy(); 
  27.                 } 
  28.             } 
  29.             private function isCameraBusy():void 
  30.             { 
  31.                 this.intervalId=setInterval(callback,50); 
  32.             } 
  33.             private function callback():void 
  34.             { 
  35.                 trace("currentFPS=" + camera.currentFPS.toString()); 
  36.  
  37.                if (camera.currentFPS > 0) 
  38.                 { 
  39.                     //CuPlayer.com提示视频设备可用 
  40.                     clearInterval(this.intervalId); 
  41.                     this.isBusyCamera=false; 
  42.                     Alert.show("CuPlayer.com提示摄像头正常"); 
  43.                 } 
  44.                 else 
  45.                 { 
  46.                     times++; 
  47.                     trace("times=" + times.toString()); 
  48.  
  49.                     if (times > 30) 
  50.                     { 
  51.                         //CuPlayer.com提示视频设备忙 
  52.                         clearInterval(intervalId); 
  53.                         this.isBusyCamera=true; 
  54.                         Alert.show("CuPlayer.com提示摄像头被占用"); 
  55.                     } 
  56.                 } 
  57.             } 
  58.              private function cl():void 
  59.             { 
  60.                 Alert.show(this.camera.currentFPS.toString()); 
  61.             } 
  62.         ]]> 
  63.     </mx:Script> 
  64.     <mx:VideoDisplay   id="vdpaly"  click="cl()" x="44" y="64" width="232" height="236"/> 
  65. </mx:WindowedApplication> 

 代码说明:

camera == null,那么就是没安装摄像头

如果摄像头被占用,那么camera.currentFPS 肯定不会大 于0,而是等于0

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器