Flash/Flex学习笔记(27):摄像头/麦克风的视频/音量指示器
在一些实时视频或视频分享应用中,需要动态显示麦克风的音量大小,或者检测视频是不是正在播放,这里演示一种简单的音量指示器
1.先写一个指示器类
其实就是一个根据百分比来填充的矩形
01 package {
02 import flash.display.Sprite;
03
04 //音量指示器(by 菩提树下的杨过 http://yjmyzz.cnblogs.com/)
05 public class Indicator extends Sprite {
06
07 private var _w:uint;
08 private var _h:uint;
09 private var _bColor:uint;
10 private var _bgColor:uint;
11 private var _fColor:uint;
12 private var _current:Number;
13
14 public function Indicator(w:uint=150,h:uint=15,borderColor:uint=0x000000,bgColor:uint=0xffffff,fillColor:uint=0xff6600,current:Number=0.3):void {
15 //trace("Indicator");
16 this._w=w;
17 this._h=h;
18 this._bColor=borderColor;
19 this._bgColor=bgColor;
20 this._fColor=fillColor;
21 this._current=current;
22 if (_current>=1) {
23 _current=1;
24 } else if (_current<=0) {
25 _current=0;
26 }
27 init();
28 }
29
30
31
32 public function set Current(v:Number):void {
33 _current=v;
34 if (_current>=1) {
35 _current=1;
36 } else if (_current<=0) {
37 _current=0;
38 }
39 init();
40 }
41
42 public function get Current():Number {
43 return (_current);
44 }
45
46
47
48 private function init():void {
49 graphics.clear();
50 graphics.lineStyle(1,_bColor);
51
52 //先画背景色
53 graphics.beginFill(_bgColor);
54 graphics.drawRect(-1*_w/2,-1*_h/2,_w,_h);
55 graphics.endFill();
56
57 //再画当前值
58 graphics.lineStyle(1,_bColor,0);
59 graphics.beginFill(_fColor);
60 var _per:Number=_w*_current;
61 graphics.drawRect(-1*_w/2 +0.5 ,-1*_h/2 +0.5,_per,_h-1);
62 graphics.endFill();
63
64 }
65 }
66 }
2.如何获取音量大小以及监测摄像头直播状态
音量大小可以通过activityLevel属性获得,但摄像头的画面变化程度却无法直接获取,但每当摄像头画面有活动时ACTIVITY事件将被触发,所以可在该事件中监测最后一次活动的时间与当前时间做比较,从而判断画面有多久没有变化了。
001 var videoInd:Indicator=new Indicator(100,10,0x000000,0xffffff);
002 var audioInd:Indicator=new Indicator(100,10,0x000000,0xffffff);
003
004 addChild(videoInd);
005 addChild(audioInd);
006
007 //初始化视频/音频指示器位置
008 videoInd.x=stage.stageWidth/2 + 30;
009 videoInd.y=-100;
010 audioInd.x=stage.stageWidth/2 + 30;
011 audioInd.y=-100;
012
013 txtMsg.y = stage.stageHeight/2 - txtMsg.height/2;
014 txtMsg.width = stage.stageWidth;
015
016
017 var cam:Camera;
018 var mic:Microphone;
019 var video:Video;
020 var videoIsWorked=false;
021 var lastVideoActiveData:Date = new Date();
022 var timer:Timer = new Timer(100,20);//每隔100ms检测摄像头状态,一共检测20次
023
024 function startCheckVideo() {
025 cam=Camera.getCamera();
026 if (cam==null) {
027 txtMsg.text="未安装摄像头!";
028 return;
029 }
030 cam.addEventListener(StatusEvent.STATUS, statusHandler);
031 cam.addEventListener(ActivityEvent.ACTIVITY,camActivityHandler);
032 video=new Video(cam.width,cam.height);
033 //trace("视频宽度:" + cam.width + ",视频高度:" + cam.height);
034 video.height = 120;
035 video.width = 160;
036 video.x=stage.stageWidth/2-video.width/2;
037 video.y=10;
038 video.attachCamera(cam);//执行这句时,flash才会弹出摄像头是否允许使用提示框
039 }
040
041 //摄像头有活动时,被触发
042 function camActivityHandler(e:ActivityEvent) {
043 //trace("camActivityHandler:" + new Date().toString());
044 if (!videoIsWorked) {
045 timer.addEventListener(TimerEvent.TIMER,TimerHandler);
046 timer.addEventListener(TimerEvent.TIMER_COMPLETE,timerCompleteHandler);
047 timer.start();
048 }
049 lastVideoActiveData = new Date();//记录本次视频活动时间
050 }
051
052 //回调函数
053 function TimerHandler(e:TimerEvent):void {
054 txtMsg.text="摄像头视频获取中..." ;
055
056 if (cam.currentFPS>0) {
057 timer.stop();
058 addChild(video);//加载到当前舞台中
059 videoInd.y=video.y+video.height+10;
060 audioInd.y=videoInd.y + videoInd.height + 10;
061 txtVideo.text = "视频状态:"
062 txtVideo.y = videoInd.y - 8
063 txtVideo.x = videoInd.x - videoInd.width -20 ;
064 txtAudio.text = "音频状态:"
065 txtAudio.y = audioInd.y - 8
066 txtAudio.x = audioInd.x - audioInd.width -20 ;
067 if (txtMsg!=null) {
068 removeChild(txtMsg);
069 }
070 videoIsWorked=true; //摄像头工作正常
071 this.addEventListener(Event.ENTER_FRAME,EnterFrameHandler);
072 }
073
074 }
075
076 function timerCompleteHandler(e:TimerEvent):void{
077 txtMsg.text="设备无法使用(有可能被占用)";
078 }
079
080 //用户选择"同意"或"不允许"使用摄像头时触发
081 function statusHandler(e:StatusEvent) {
082 if (e.code=="Camera.Muted") {
083 txtMsg.text="您不允许使用摄像头!";
084 } else if (e.code == "Camera.Unmuted") {
085 txtMsg.text="摄像头视频获取中...";
086 camActivityHandler(null);//重要:在release模式下,webCam的Activity事件不会自动触发(Adobe的bug?),所以要显式手动调用一次
087 }
088 }
089
090 //开始检测麦克风
091 function startCheckAudio():void{
092 mic = Microphone.getMicrophone();
093 mic.setLoopBack(true);//将采集到的声音回发到扬声器(否则自己听不到自己的声音,不方便测试)
094 }
095
096 function EnterFrameHandler(e:Event):void{
097 var curDate:Date = new Date();
098 var _timeSpan:Number = curDate.time - lastVideoActiveData.time;
099 if (_timeSpan >= 30 * 1000){
100 trace("您的视频已经超过30秒没动静了!");
101 videoInd.Current = 0;
102 }
103 else{
104 videoInd.Current = Math.random() * 0.1;//视频指示器
105 }
106 if (mic!=null){
107 audioInd.Current = 0.05 + mic.activityLevel/100;//音量指示器
108 }
109 }
110
111 txtMsg.text="正在检测摄像头...";
112 startCheckVideo();
113 startCheckAudio();
源文件:http://cid-2959920b8267aaca.skydrive.live.com/self.aspx/Flash/webCamStatus.rar
浙公网安备 33010602011771号