硬件条件:一个摄像头 + 一台FMS服务器即可
原理:摄像头实时采集视频源,然后推送到FMS服务器,其它客户从FMS上获取视频流。
需要做二个fla,一个用于向FMS服务端推送视频源(即发送端),一个用于从FMS服务器获取并播放视频(即接收端)
Server.fla关键代码:
01
var
cam:Camera;
02
var
vod:Video;
03
var
intervalId:
uint
;
04
var
intelvalTimes:
uint
=
0
;
05
var
isWorked=
false
;
06
var
nc:NetConnection;
07
var
ns:NetStream;
08
09
btnPublish.addEventListener(MouseEvent.CLICK,btnPublishClick);
10
lblMsg.text =
"连接好摄像头后,\n点击下面的按钮"
11
12
//摄像头有活动时,被触发
13
function
camActivityHandler(e:ActivityEvent) {
14
trace
(e);
15
if
(!isWorked) {
16
intervalId=setInterval(checkCallBack,
100
);
17
}
18
}
19
20
function
checkCallBack():
void
{
21
intelvalTimes+=
1
;
22
trace
(intelvalTimes);
23
if
(cam.currentFPS>
0
) {
24
lblMsg.text=
"摄像头工作正常!"
;
25
isWorked=
true
;
26
clearInterval(intervalId);
27
ns.publish(
"myCamera"
,
"live"
);
28
//同时处理本地播放
29
vod =
new
Video();
30
vod.attachCamera(cam);
31
vod.height=cam.height;
32
vod.width=cam.width;
33
vod.x =
45
;
34
vod.y =
15
;
35
addChild(vod);
36
}
else
{
37
if
(intelvalTimes>=
10
) {
//持续检测2秒,仍然无图象的话,认为"设备无法使用(占用中)"
38
lblMsg.text=
"设备无法使用(有可能被占用)"
;
39
clearInterval(intervalId);
40
}
41
}
42
}
43
44
//用户选择"同意"或"不允许"使用摄像头时触发
45
function
statusHandler(e:StatusEvent) {
46
trace
(e);
47
if
(e.code==
"Camera.Muted"
) {
48
lblMsg.text=
"您不允许使用摄像头!"
;
49
}
else
if
(e.code ==
"Camera.Unmuted"
) {
50
lblMsg.text=
"摄像头视频获取中..."
;
51
}
52
}
53
54
//推送按钮点击事件
55
function
btnPublishClick(e:MouseEvent) {
56
nc =
new
NetConnection();
57
nc.addEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler);
58
nc.connect(
"rtmp://fms服务器ip地址/camlive"
);
59
}
60
61
//连接状态处理代码
62
function
NetStatusHandler(event:NetStatusEvent):
void
{
63
trace
(event.info.code);
64
if
(event.info.code==
"NetConnection.Connect.Success"
) {
65
cam=Camera.getCamera();
66
if
(cam==
null
) {
67
this
.lblMsg.text=
"未安装摄像头!"
;
68
return
;
69
}
70
cam.addEventListener(StatusEvent.STATUS, statusHandler);
71
cam.addEventListener(ActivityEvent.ACTIVITY,camActivityHandler);
72
ns=
new
NetStream(nc);
73
ns.attachCamera(cam);
74
}
75
}
Client.fla关键代码:
01
var
nc:NetConnection;
02
var
ns:NetStream;
03
var
vod:Video;
04
05
btnReceive.addEventListener(MouseEvent.CLICK,
function
(){
06
nc =
new
NetConnection();
07
nc.addEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler);
08
nc.connect(
"rtmp://fms服务器ip地址/camlive"
);
09
})
10
11
//连接状态处理代码
12
function
NetStatusHandler(event:NetStatusEvent):
void
{
13
trace
(event.info.code);
14
if
(event.info.code==
"NetConnection.Connect.Success"
) {
15
lblMsg.text =
"正在获取视频..."
16
ns=
new
NetStream(nc);
17
ns.addEventListener(NetStatusEvent.NET_STATUS,nsNetStatusHandler);
18
ns.play(
"myCamera"
);
19
vod =
new
Video();
20
vod.x=
45
;
21
vod.y=
15
;
22
vod.height=
120
;
23
vod.width=
160
;
24
vod.attachNetStream(ns);
25
addChild(vod);
26
}
27
}
28
29
function
nsNetStatusHandler(e:NetStatusEvent){
30
trace
(e.info.code);
31
}
在线演示: