在Flex 迷你教程 — 基于Stratus的P2P网络电话 (2)中我们已经知道如何链接一个客户端并且互相发送短信,今天来扩展下面的这些内容
- 呼叫时在被呼叫端显示 “接受”,点击接受后双方建立链接。
- 呼叫试或者接通后可以挂机。
- 添加视频与音频通信。
首先来看看Demo:
Demo 操作:
1. 输入任意名称,点击链接 (两台机器或者两个浏览器)
2. 输入对方的peerId,点击呼叫
3. 被呼叫方点击 “接受” 建立链接。
4. 开始语音,视频,文字通讯.
5. 挂机
Demo地址:
http://flextheworld.com/flash/p2p/P2pPhoneDemo.html
Demo截图:

新添加的代码
首先是修改call() function, 根据不同的label执行不同的事件,如果callYou.label是 “接受”,执行accpetHandle()与呼叫者建立链接,如果是”挂断”,那么执行hangup()挂断双方的通讯,如果是其他(也就是”呼叫”),则执行后面的内容。
呼叫时调用publishVide和publishAudio将视频与音频附加到outgoingStream发送。在onConnectSuccesss事件中播放被呼叫者返回的视频和音频。监听onHangup事件来控制挂断。呼叫时将“呼叫”改为“挂断”。
//呼叫对方的方法,现在是呼叫者角色 private function call():void{ if(callYou.label == "接受"){ accpetHandle(); callYou.label = "挂断" return; }else if(callYou.label == "挂断"){ outgoingStream.send("onHangup") this.hangup() return; } ............... //发送视频 publishVideo(); //发送音频 publishAudio() .......... //监听onConnectSuccess事件,确定链接成功 i.onConnectSuccess = function(name:String):void { info.text += "与"+name + "链接成功n"; sendMessageBtn.enabled = true; incomingStream.receiveAudio(true) incomingStream.receiveAudio(true) remoteVideo = new Video(); remoteVideo.width = 160; remoteVideo.height = 120; remoteVideo.attachNetStream(incomingStream); remoteVideoDisplay.addChild(remoteVideo); sendMessageBtn.enabled = true; } i.onHangup = function():void{ hangup() } incomingStream.client = i callYou.label = "挂断" |
被呼叫者在监听到呼叫者的呼叫时不马上回应,只是提示被呼叫者 “正在被链接”,同时将“呼叫”改为“接受”, 监听onHangup事件
private function initSendStream():void{ .......... //监听onPeerConnect事件 var o:Object = new Object(); o.onPeerConnect = function(subscriberStream:NetStream):Boolean { .......... incomingStream.receiveAudio(false) incomingStream.receiveAudio(false) .......... //监听onIncomingCall事件,用于确定链接成功 i.onIncomingCall = function(name:String):void { //显示链接成功后,对呼叫者发布我的信息流,名称为callee info.text += name + " 正在呼叫你n"; callYou.label = "接受" } i.onHangup = function():void{ hangup() } incomingStream.client = i; return true; } myStream.client = o; } |
点击“接受”后,开始向呼叫者发送信息流,包括视频与音频。同时播放呼叫者发送的视频与音频,最后发出onConnectSuccess事件
//同意链接后,发送stream到呼叫端,包括音频,视频的发送。 同时播放呼叫端发出的视频,音频 private function accpetHandle():void{ //向呼叫端发送信息流 outgoingStream = new NetStream(netConnection, NetStream.DIRECT_CONNECTIONS); outgoingStream.addEventListener(NetStatusEvent.NET_STATUS, outgoingStreamHandler); outgoingStream.publish("callee"); //将视频,音频附加到outgoingStream publishAudio(); publishVideo() //播放接受到的视频,音频 incomingStream.receiveAudio(true) incomingStream.receiveAudio(true) remoteVideo = new Video(); remoteVideo.width = 160; remoteVideo.height = 120; remoteVideo.attachNetStream(incomingStream); remoteVideoDisplay.addChild(remoteVideo); //链接成功事件,呼叫端响应 outgoingStream.send("onConnectSuccess",userName.text); sendMessageBtn.enabled = true; } |
点击挂机后在上面提到的call()中执行hangup(), 同时发送onHangup事件,这样另一方也能挂断电话。挂机后将”挂机”再改为”呼叫”,允许下一次的呼叫和链接
//挂断电话,清空所有stream,只保持于stratus的链接 public function hangup():void{ if (incomingStream){ incomingStream.close(); incomingStream.removeEventListener(NetStatusEvent.NET_STATUS, incomingStreamHandler); } if (outgoingStream){ outgoingStream.close(); outgoingStream.removeEventListener(NetStatusEvent.NET_STATUS, outgoingStreamHandler); } if (controlStream){ controlStream.close(); controlStream.removeEventListener(NetStatusEvent.NET_STATUS, netStreamHandler); } incomingStream = null; outgoingStream = null; controlStream = null; if(localVideo != null){ localVideo.attachCamera(null) } if(remoteVideo != null){ remoteVideo.attachCamera(null) } info.text += "挂机!" callYou.label = "呼叫" sendMessageBtn.enabled = false; } |
publishVideo和publishAudio function
//发送视频 private function publishVideo():void{ var cameras:Array = Camera.names; var camera:Camera; var findDefaultCamera:Boolean = false if (cameras && cameras.length >0){ for(var j:int=0; j<cameras.length; j++){ if(cameras[j].toString() == "USB Video Class Video"){ camera = Camera.getCamera(j.toString()) outgoingStream.attachCamera(camera); findDefaultCamera = true } } if(!findDefaultCamera){ camera = Camera.getCamera() outgoingStream.attachCamera(camera) } camera.setQuality(0,90) localVideo = new Video(); localVideo.width = 160; localVideo.height = 120; localVideo.attachCamera(camera) localVideoDisplay.addChild(localVideo); } } //发送音频 private function publishAudio():void{ var mics:Array = Microphone.names; if (mics && mics.length >0){ outgoingStream.attachAudio(Microphone.getMicrophone(0)) } } |

支持一下,下一个看看,谢谢
Reply
Kevin Luo Reply:
January 18th, 2009 at 9:35 PM
谢谢
Reply
Jason Reply:
April 16th, 2009 at 1:44 PM
支持,谢谢分享!!
Reply
kyk Reply:
August 13th, 2009 at 5:28 PM
支持 學習!!
Reply
不错~.~
Reply
Kevin Luo Reply:
January 18th, 2009 at 9:40 PM
谢谢
Reply
演示没带音频?
Reply
Kevin Luo Reply:
January 18th, 2009 at 10:27 PM
演示带音频的,不过因为默认打开index为0 的麦克风,有可能你的麦克风索引不在0,所以开启没有成功,你可以查看代码中publishAudio() 部分代码。
Reply
嘻嘻,这个东东可以每天打开监视你
Reply
能支持多少并发用户?
Reply
我的笔记本(自带摄像头) 连键就报错误
台机可以连接呀
Reply
Kevin Luo Reply:
January 21st, 2009 at 11:09 AM
噢?报的什么错? 因为默认找的是 名称叫 “USB Video Class Video”的摄像头,如果找不到的话默认加载index为0的摄像头。这里是有可能出错。
Reply
很不错
Reply
这个不错 ,可惜不能穿内网
Reply
Kevin Luo Reply:
January 23rd, 2009 at 9:55 AM
如果内网有防火墙的话需要设置
Reply
谢谢
Reply
下来测试下~~~
Reply
好,下来测试下~玩玩
Reply
下来看看…
Reply
好东西啊,支持
Reply
Kevin Luo Reply:
February 6th, 2009 at 6:11 PM
谢谢支持
Reply
支持
Reply
look so good
Reply
不错。。这个挺实用的,谢谢楼主。体会到了flex的强大功能。。呵呵。虽然没有i/o操作。
Reply
好东西要看看
Reply
good,先下了。谢谢
Reply
学习一下
Reply
能下吗?
Reply
我觉得很好,我想下载用用
Reply
sang Reply:
February 19th, 2009 at 9:28 AM
可耕地
Reply
很好 很强大
Reply
谢谢博主提供演示代码,正好补充自己的知识~
Reply
谢谢
Reply
出来打酱油
Reply
想要源文件 thank you
Reply
nice
Reply
好!
Reply
Gut!
Reply
不错
Reply
good
Reply
支持啊!太爽了
Reply
很强大
Reply
正需要,谢谢
Reply
学习了 很好啊
Reply
学习了
Reply
Sehr gute Seite. Ich habe es zu den Favoriten.
Reply
kankankankan
Reply
thank you for your sharing
Reply
thx~
Reply
我想你也知道adobe提供的那段p2p视频系统代码啊,我想问个问题,我的编译不过去,总是有很多错误,如下
mic.codec = SoundCodec.SPEEX;
//提示没有soundcodec的定义,我想是版本的问题,我的sdk是3.0.0的。你能告诉我为什么吗?已经忙了很多天了,就是不知道错误所在。在as 3.0中我也找到了定义。
Reply
study!!
Reply
不错,支持
Reply
非常之好
Reply
测试下
Reply
好东西,多谢
Reply
我也要看看啊
Reply
研究下代码~~
Reply
下载,谢谢
Reply
呵呵 ,不错,谢谢分享
Reply
Goooood….
Reply
支持一下,谢谢啦。
Reply
awesome
Reply
想问p2p能不能一对多,能不能文件传送呢
Reply
谢谢
Reply
kankanzenmeyang
Reply
这个要好好学习下
Reply
很不错
Reply
hao!
Reply
good
Reply
看看怎么实现的,我从adobe网站也下了个,就是运行不起来!
Reply
真好
Reply
非常好
Reply
支持!
Reply
xiexie
Reply
Reply
Reply
多谢分享,谢谢!!支持
Reply
谢谢
Reply
不错,试试。
Reply
不错,学习中
Reply
sdfsdfsdf
Reply
学习
Reply
谢谢
Reply
test interest
Reply
逼我
Reply
看看怎么实现的,我从adobe网站也下了个,就是运行不起来!
Reply
这个要看
Reply
ding
Reply
Very good
Reply
法萨awsfasdf
Reply
hhh
Reply
看看
Reply
kan kan
Reply
很感兴趣, 谢谢
Reply
gooooood joooooob
Reply
我也需要源代码
Reply
tks hehe
Reply
hehe 我很想要源码 只是stream 太多 我晕了
Reply
学习下。
Reply
学习下,AIR教程中某些例子下不下来了。。
还是要谢谢。
Reply
学习,好玩
Reply
good verygood
Reply
不错哦。。。很好。。。
Reply
good!
Reply
呓 止境昌盛
Reply
每次都要双方取得ID,再发给对方,才O!有些烦琐!
Reply
j
Reply
看看
Reply
下载学习
Reply
非常不错,学习下
Reply
SHIYIXIA
Reply
3ks
Reply
真是一个好东西,现在正好要用到。
Reply
真是一个好东西,现在正好要用到
Reply
开源万岁,谢谢分享
Reply
you are very good
Reply
还是有局限性
Reply
bucuo xiexie
Reply
很期待
Reply
期待不会等待
Reply
daaadaaa
Reply
asdsadasdasd
Reply
實在太利害了
Reply
怎麼不能下載 ? anyway , thanks a lot !
Reply
asdfasdfa
Reply
好东西要要
Reply
只能回复看看了
Reply
好,下来测试下~玩玩
Reply
实在太好了。谢谢你。
Reply
研究一下
Reply
good good good
Reply
goog
Reply
let me see
Reply
用户名与peer ID之间的转换,如果实现呢,请麻烦您解答下。
因为每次用这个id太麻烦了,用用户名比较方便
Reply
Kevin Luo Reply:
June 11th, 2009 at 4:40 PM
需要用服务器来维护peedID与用户名之间的转换,单靠客户端是做不到的
Reply
还有就是如果在该功能上加入白板 请问能做吗?怎么做能不能指导下?
Reply
Kevin Luo Reply:
June 11th, 2009 at 4:42 PM
可以加白板,做法和传送message一样,把这边的白板信息传给对方(可以是坐标或者任何能表示内容的对象),接收方解析来的对象在自己的白板上自动绘出内容。
Reply
good articles
Reply
下个学习一下
Reply
看来这的留言最多了
Reply
AASAA
Reply
很关心,看看
Reply
学习一下
Reply
想看看
Reply
不错
Reply
谢谢,下来看看
Reply
支持!
Reply
好像不支持局域网的机器哦,不知道能不能通过设置代理来连接服务器?
Reply
我正在自学Flex
Reply
好东西要看看
Reply
想看看终极版本
Reply
thx
Reply
it`s so good .i need codesource to stude flex.
Reply
great! I need the source code,ok?
Reply
thanks
Reply
jtyjtjt
Reply
支持
Reply
谢谢
Reply
教程不错!~
Reply
不错的!
Reply
还不错啊~
Reply
das Reply:
July 6th, 2009 at 3:51 PM
qweqwe
Reply
真的很不错
Reply
想看下完整的代码,O(∩_∩)O~
Reply
请问下这东西如果有防火墙的话需要这么设置啊
Reply
不错不错
Reply
下来看看
Reply
顶起
Reply
很好玩吖~
Reply
很好喔 好例子
Reply
非常好!
Reply
正在研究Adobe P2P相关的技术,很感谢能有这篇博文
Reply
学习学习
Reply
打酱油!
Reply
哈哈,原来有高手
Reply
TEST
Reply
我看了您的代码,对我很有帮助。
怎么才能实现多对一的聊天呢?能否提供个思路。
Reply
真厉害!!!
Reply
22
Reply
很好,谢谢你。
Reply
sdfsdfdsfsdf
Reply
很好很强大….
Reply
谢谢你的分享.~!~
Reply
好
Reply
不错啊,学习一下
Reply
googd
Reply
看看
Reply
很不错,。强大
Reply
赞一个
Reply
还好
Reply
看卡
Reply
下来看看,只是不知道能不能用于局域网啊
Reply
Greate
Reply
vectors douglass economists estimated data shelf million emitted [url=http://journals.cambridge.org]special china gas beginning negative[/url] http://ideas.repec.org
Reply
hao
Reply
回了
Reply
nice
Reply
瞧瞧文件哦
Reply
值得学习的东东
Reply
值得学习的东东
Reply
dddddddddddddddddddddddddd
Reply
dsfsdfsd
Reply
研究看看
Reply
哈哈,good 好东西
Reply
好棒的教學
Reply
太厉害了,我现在一头雾水在。。。。偶像啊
Reply
貌似有点问题
Reply
It is good
Reply
顶大哥!呵呵!真的是好东西,不知道大哥还记得我不?
Reply
Kevin Reply:
September 21st, 2009 at 9:53 AM
小新嘛,杂会不记得呢,呵呵。最近太忙了都很少时间去坛子了。
Reply
ks很好,很强大
Reply
很好,试试!
Reply
learning
Reply
不错 稍微有点回音,不过很清晰
Reply
fds
Reply
sdfsdfsdf
Reply
It helps me a lot!
Thx!
Reply
受益匪浅。
Reply
very good,learn it
Reply
学习
Reply
just download to study
Reply
reply
Reply
very good,learn it
Reply
看看
Reply
as
Reply
ratified reduction high era deep article [url=http://www.ppnt.org]led permafrost reliable cycles[/url] http://www.gizapyramid.com
Reply
ergeer
Reply
very good,learn it
Reply
dfssd
Reply
teste
Reply
hehe
Reply
ok.thank you,
Reply
很好
Reply
DSFDSF
Reply
HEN BU CUO
Reply
网页文字显示的有些乱.希望改进!
Reply
huifu
Reply
just look
Reply
学习中
Reply
学习下
Reply
THX YOU SO MUCH
Reply
THX YOU SO MUCH
Reply
THX YOU SO MUCH
Reply
看看
Reply
let me try
Reply
let me try
Reply
谢谢
Reply
dfsdfsdfsdfsdfdf
Reply
hehe,have a look!
Reply
谢谢博主分享,学习了~~
Reply
aa
Reply
Reply
Reply
yes~~~this is a great tutorial that i have never seen!!
Reply
很有帮助,真是感谢!!!
Reply
thanks!!!
Reply
is so good
Reply
不错。。
Reply
Study!
Reply
学习
Reply
很有帮助!
Reply
eeeeeeeeeeeeee
Reply
看看…谢谢了..LZ好棒的
Reply
太感谢了
Reply
fdsa
Reply
谢谢你的文章
Reply
地方 Reply:
January 15th, 2010 at 9:58 PM
ok.thx
Reply
flex ok
Reply
很不错的学习资料,谢谢了
Reply
flash p2p 的應用 真感謝你的文章^^
Reply
呵呵。谢谢~~
Reply
…………
Reply
非常有用 谢谢
Reply
谢谢
Reply
thank you very much
Reply
thank you very much
Reply
哈哈,要拿来学习下
Reply
fffffffffffffffffff
Reply
flex rocked!!!!!!!!!!!!
Reply
感谢楼主提供这样的源码,对于我这样的初学者来说帮助很大,谢谢
Reply
Kevin Luo Reply:
December 22nd, 2009 at 5:04 PM
Reply
Batuhan KAHRAMAN Reply:
February 10th, 2010 at 5:37 AM
hi mr luo ;
I am use your p2p3 application.
Thank you for sharing code …
But I noticed a small problem…
I tested this story ;
Step 1 : Open separately 3 ie screen … connect from a,b,c users …
Step 2 : a user call b user
Step 3 : b user accepted call
Step 4 : a and b chating … no problem ….
Step 5 : c user call a or b …
Step 6 : a disconnect b or b disconnect a … problem with c user
do you understand me sir…
Reply
Kevin Luo Reply:
February 10th, 2010 at 10:01 AM
Hi Batuhan, I know what’s your mean, I did not handle this situation in demo. Basically it’s just a quite simple demo to show you how the p2p work, of course you need develop it again base on your project.
reply
Reply
谢了
Reply
不错的文章
Reply
asdwqeqweqaasdwqeads
Reply
谢谢!
Reply
fafafaf
Reply
呵呵,写的不错,正在学这个呢
Reply
哇
Reply
很好
Reply
thank you
Reply
谢谢了哈哈
Reply
研究学习下…
Reply
I want your code very much
Reply
g
Reply
博主何必看重回复这种虚无缥缈的东西呢?而且是被动回复
Reply
谢谢,非常喜欢
Reply
不错
Reply
good.
Reply
一直尝试做这方面的开发,这个子错
Reply
thanks for you
Reply
谢谢
Reply
现在有个问题,假设有两个用户AB,
一开始B呼叫A,A接收了
然后A再点一次连接,这是他的peerid已经改变
为什么AB还是连接在一起的,还能相互发消息?
不是说他们的stratus是根据peerid来确认的吗?
Reply
Kevin Reply:
January 15th, 2010 at 2:58 PM
因为他们建立链接以后peerid就没用了,peerid只是用来建立两台机器的链接是起作用,一旦两台机器联通,peerid就不发挥作用了,机器直接的传输是UPD协议。
Reply
如何实现一对多聊天?
Reply
非常有用 谢谢
Reply
好东西哈 谢谢 了
Reply
不错,可以
Reply
谢谢了
Reply
看看先,
Reply
Good!
Reply
非常好,谢谢
Reply
Great job.
Reply
不错的程序,学习中
Reply
图中的两幅图是用摄像头得到的吗?
Reply
Kevin Luo Reply:
January 28th, 2010 at 11:33 AM
那两张图是用来挡住摄像头真实画面的
Reply
Gluttony Reply:
January 28th, 2010 at 2:17 PM
…还以为可以上传图片
Reply
testt
Reply
It is cool,Thanks!!!
Reply
请问博主
有没有组播相关的研究呢?
Reply
Kevin Luo Reply:
March 4th, 2010 at 2:59 PM
http://www.flextheworld.com/2010/02/flex-stratus-2-p2p.html. 这一篇就是了 :)
Reply
回复的真快啊,帖子还是热乎的,赶紧研究一下。
前面的教程非常精彩,有问题再请教了,谢谢。
Reply
If you want to buy a house, you would have to get the personal loans. Furthermore, my mother always utilizes a small business loan, which occurs to be the most firm.
Reply
请教一些问题,
1,flash的p2p穿透是不是还有些问题,自己写的一对一的测试代码在有些环境下无法连接
2,stratus的开发key一次能接受多少少连接数,如果要用到生产环境应该用什么方案fms4? red4支持RTMFP吗,
万分感谢
Reply
Kevin Luo Reply:
April 23rd, 2010 at 12:42 PM
1. 如果被连接方防火墙禁止upd传输,会有穿透问题。
2. 链接数应该是没有限制的。FMS4据我所知现在是内测阶段,具体的信息还不是很清楚,应该会继承Stratus service的功能。 red5不支持RTMFP
Reply
okfeng Reply:
June 21st, 2010 at 6:01 PM
关于p2p穿透的问题,如果对方防火墙禁止udp传输,那有没有办法解决呢?
我看Adobe的技术资料,是说RTMFP支持UDP的防火墙穿越的。
谢谢!
Reply
tabaga Reply:
June 6th, 2010 at 10:08 PM
好,学习一下
Reply
好吧,要回复
Reply
我在跑这个例子的时候,发现在ie8下面,被呼叫一方不能显示自己的视频图像,呼叫一方也显示不到被呼叫一方的图像。但是在ff和chrome下面就没问题,能帮忙解释下么?谢谢~
Reply
Kevin Luo Reply:
May 27th, 2010 at 5:38 PM
是同一个机器还是两台机器测试的?
Reply
duran Reply:
May 28th, 2010 at 9:53 AM
在同一台机器,浏览器开得两个标签页
Reply
Kevin Luo Reply:
May 28th, 2010 at 9:58 AM
那是正常的,是ie的问题。不同机器就没事了
wangdelin Reply:
July 12th, 2010 at 11:20 AM
我也遇到这个问题,我用的是遨游跟ie8测试的,遨游没有问题ie8有问题,不过遨游不是也用的ie的内核么
????
Reply
???
Reply
怎么没速度啊
Reply
//监听onPeerConnect事件
var o:Object = new Object();
o.onPeerConnect = function(subscriberStream:NetStream):Boolean
{
……….
incomingStream.receiveAudio(false)
incomingStream.receiveAudio(false)
还有这个段代码执行了2次receiveAudio(false),是楼主写错了,还是有一句应该改成receiveVideo(false)?
Reply
Kevin Luo Reply:
July 28th, 2010 at 4:50 PM
恩,这里写错了。谢谢纠正
Reply
Louis Vuitton
Gucci Shoes
Hermes Handbags
christian louboutin
nike shoes
replica bags
Louis Vuitton
Gucci Bags
Cheap gucci shoes
wholesale gucci shoes
louis vuitton
herve leger
Rolex Watches
Omega Watches
louis vuitton multicolore
louis vuitton vernis
Reply
能否稍微详细的点播一下怎么获取所有在线的用户列表?
Reply
Kevin Luo Reply:
July 28th, 2010 at 4:51 PM
在线用户列表实际上是用服务器端维护的,服务器端不需要维护用户链接,但是需要维护那些用户在线,简单来说就是用户获取peerid以后保存到服务器。
Reply
ZZK Reply:
July 29th, 2010 at 10:10 PM
如果要做到其他用户登录即使显示的话,是不是应该sockets更好一点。
Reply
Kevin Luo Reply:
July 29th, 2010 at 10:26 PM
你说用socket链接各个客户端? 一样的问题,你的用户列表存哪?新加入的客户端怎么知道其他客户端的地址? 服务器就是干这么活的,维护在线列表,告诉新来的现在有哪些客户端可以连接
请教下大哥 我可以用这个发送文件吗 像QQ文件对传一样 该怎么做呢?如果能给个demo小弟感激不尽
Reply
[...] Flex 迷你教程 — 基于Stratus的P2P网络电话 (3) [...]
好东西,学习了。
Reply
多谢!!
Reply