Flash/Flex学习笔记(11):如何检测摄像头是否被占用

原理:摄像头激活后,持续检测与之关联的Video对象是否在播放(即每秒帧数)

view source

print?

01
btnCheck.addEventListener(MouseEvent.CLICK,btnCheckClick);

02

03
var cam:Camera;

04
var video:Video;

05
var intervalId:uint;

06
var intelvalTimes:uint=0;

07

08
function btnCheckClick(e:MouseEvent) {

09
cam=Camera.getCamera();

10
if (cam==null) {

11
lblResult.text="未安装摄像头!";

12
return;

13
}

14
cam.addEventListener(StatusEvent.STATUS, statusHandler);

15
cam.addEventListener(ActivityEvent.ACTIVITY,camActivityHandler);

16
video=new Video(cam.width,cam.height);

17
//trace("视频宽度:" + cam.width + ",视频高度:" + cam.height);

18
video.x=10;

19
video.y=40;

20
video.attachCamera(cam);//执行这句时,flash才会弹出摄像头是否允许使用提示框 

21
}

22

23
//摄像头有活动时,被触发

24
function camActivityHandler(e:ActivityEvent) {

25
trace(e);

26
intervalId=setInterval(checkCallBack,100);

27
}

28

29
function checkCallBack():void {

30
intelvalTimes+=1;

31
trace(intelvalTimes);

32
if (cam.currentFPS>0) {

33
lblResult.text="摄像头工作正常!";

34
clearInterval(intervalId);

35
addChild(video);//加载到当前舞台中  

36
} else {

37
if (intelvalTimes>=20) {//持续检测2秒,仍然无图象的话,认为"设备无法使用(占用中)"

38
lblResult.text="设备无法使用(有可能被占用)";

39
clearInterval(intervalId);

40
}

41
}

42
}

43

44
//用户选择"同意"或"不允许"使用摄像头时触发

45
function statusHandler(e:StatusEvent) {

46
trace(e);

47
if (e.code=="Camera.Muted") {

48
lblResult.text="您不允许使用摄像头!";

49
} else if (e.code == "Camera.Unmuted") {

50
lblResult.text="摄像头视频获取中...";

51
}

52
}

posted @ 2010-11-22 16:25  模西的哥哥  阅读(644)  评论(0编辑  收藏  举报