这些都是很久以前玩flash的时候写的,为了怕掉,不知道以后还用没有用,毕竟是自己写的很值的留念哦!所以发上来备份一下,同时给需要的一个小小帮助!
学了as3 就接着学了fms:做了一个视频交流系统;写了3个类!
videoChat.as
package {
import flash.display.*;
import flash.text.*;
import flash.net.*;
import flash.events.*;
import flash.errors.*;
import flash.system.*;
import fl.managers.StyleManager;
import flash.media.*;
import fl.controls.List;
import fl.data.DataProvider;
import fl.controls.TextArea;
public class videoChat extends Sprite {
public var IP:String;
public var fmsPath:String;
public var myNC:NetConnection;
public var myNS:NetStream;
private var mySubNS:NetStream;
public var userName:String;
public var mySO:SharedObject;
public var peopleArray:Array;
public var tmpName:String;
public var msg:String
public var now:Date;
private var myCam:Camera;
private var myMic:Microphone;
public function videoChat() {
init();
startConnect();
}
public function init() {
var myTF:TextFormat=new TextFormat();
myTF.size=12;
StyleManager.setStyle("textFormat",myTF);//不理解的代码
IP="127.0.0.1";//不理解的代码
fmsPath="rtmp://"+IP+"/videoChat";//不理解的代码
userName="guest"+int(Math.random()*1000);
userName1.text=userName;
userName2.text="所有人";
now=new Date();
peopleList.addEventListener(MouseEvent.CLICK,selectChatTo);
peopleList.addEventListener(MouseEvent.DOUBLE_CLICK,getVideo);
}
public function startConnect() {
myNC=new NetConnection;
myNC.client=this;
myNC.objectEncoding=ObjectEncoding.AMF3;
myNC.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
myNC.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncError);
myNC.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityError);
myNC.connect(fmsPath,userName);
}
public function netStatus(event:NetStatusEvent):void {
switch (event.info.code) {
case "NetConnection.Connect.Success" :
chatContent.htmlText+="连接成功";
setMySO();
sendBtn.addEventListener(MouseEvent.MOUSE_DOWN,sendMsgByMouse);
stage.addEventListener(KeyboardEvent.KEY_DOWN,sendMsgByKey);
publishStream();
break;
case "NetConnection.Connect.Failed" :
chatContent.htmlText+="连接失败";
break;
case "NetConnection.Connect.Rejected" :
chatContent.htmlText+=event.info.application.message;
break;
}
}
public function securityError(event:SecurityErrorEvent):void{
chatContent.htmlText+="安全性错误"}
public function setMySO():void{
mySO=SharedObject.getRemote("mySO",myNC.uri,false)
mySO.addEventListener(SyncEvent.SYNC,mySoSync)
mySO.connect(myNC)}
public function asyncError(event:AsyncErrorEvent):void{}
public function mySoSync(e:SyncEvent):void{
peopleArray=new Array
for (var tmp in mySO.data){
peopleArray.push(tmp)}
var tmpDP:DataProvider=new DataProvider
for(var i=0;i<peopleArray.length;i++){
tmpName=peopleArray[i]
tmpDP.addItem({label:tmpName})}
tmpDP.sortOn("label")//很有可能有错
tmpDP.addItemAt({label:"所有人"},0)
peopleList.dataProvider=tmpDP
}
public function sendMsgByKey(event:KeyboardEvent):void{
if(event.keyCode==13){
sendMsg()}}
public function sendMsgByMouse(event:MouseEvent):void{
sendMsg()}
public function sendMsg():void{
if(willSendMsg.length>0){
if(userName2.text==userName1.text){
chatContent.htmlText+="你不能对你自己发言"+"\n"
chatContent.verticalScrollPosition=chatContent.maxVerticalScrollPosition}
else {
var allpeople:Array=new Array
for(var i=0;i<peopleList.length;i++){
if(peopleList.getItemAt(i).label!="所有人"&&peopleList.getItemAt(i).label!=userName1.text){
allpeople.push(peopleList.getItemAt(i).label)}}
msg="<font color='#ff0000'>"+userName1.text+"</font)"+"对"+"<font color='#ff0000'>"+userName2.text+"</font>"+"说"+"("+"<font color='#0000ff'>"+now.getHours()+":"+"</font>"+"\n"+willSendMsg.text+"\n"
if(userName2.text==null)
{
userName2.text="所有人"
}
myNC.call("msgFromClient",null,userName1.text,userName2.text,msg,allpeople)
chatContent.htmlText+=msg
chatContent.verticalScrollPosition=chatContent.maxVerticalScrollPosition
willSendMsg.text=""
}}}
public function msgFromSrvr(msg){
trace(msg)
chatContent.htmlText+=msg
chatContent.verticalScrollPosition=chatContent.maxVerticalScrollPosition}
private function selectChatTo(event:MouseEvent):void{
userName2.text=peopleList.selectedItem.label}
private function publishStream():void{
myCam=Camera.getCamera()
myMic=Microphone.getMicrophone()
liveVideo.attachCamera(myCam)
myNS=new NetStream(myNC)
myNS.client=this
myNS.addEventListener(NetStatusEvent.NET_STATUS,netStatus)
myNS.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncError)
myNS.attachAudio(myMic)
myNS.attachCamera(myCam)
myNS.publish(userName1.text,"live")
}
private function getVideo(event:MouseEvent):void{
if(userName2.text!=userName1.text){
whoseVideo.text=userName2.text+"的视频 "
mySubNS=new NetStream(myNC)
mySubNS.client=this
mySubNS.addEventListener(NetStatusEvent.NET_STATUS,netStatus)
mySubNS.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncError)//这个函数不是很清楚
liveVideo.attachNetStream(mySubNS)
mySubNS.play(userName2.text)}
else{
mySubNS.close()
liveVideo.clear()
whoseVideo.text="我的视频"
liveVideo.attachCamera(myCam)}}
}
}
Myself.as
package{
import flash.display.Sprite
import flash.events.MouseEvent
import flash.net.ObjectEncoding
import flash.net.NetConnection
import flash.net.NetStream
import flash.text.TextField
import fl.controls.List
public class extends Myself extends Sprite{
private var myNC:NetConnection
private var myNS:NetStream
private var userName1:String
private var userName2:String
public function Myself(){
init()
started()}
public function init(){
}}}
main.asc
application.onAppStart=function(){
trace("程序已启动")
list=new Object()
application.mySO=SharedObject.get("mySO",false)}
application.onConnect=function(newClient,userName){
trace("有新的用户连接:"+userName)
if(list[userName]==null){
list[userName]=newClient
newClient.userName=userName
this.acceptConnection(newClient)
application.mySO.setProperty(newClient.userName,userName)
newClient.msgFromClient=function(user1,user2,msg,allpeople){for(var i=0;i<allpeople.length;i++){
application.list[allpeople[i]].call("msgFromSrvr",null,msg)}}}
else {
var err=new Object()
err.message="对不起,该用户已存在"
application.rejectConnection(newClient,err)}}
application.onDisconnect=function(oldUser){
application.mySO.setProperty(oldUser.userName,null)
list[oldUser.userName]=null
delete list[oldUser.userName]}
学了as3 就接着学了fms:做了一个视频交流系统;写了3个类!
videoChat.as
package {
import flash.display.*;
import flash.text.*;
import flash.net.*;
import flash.events.*;
import flash.errors.*;
import flash.system.*;
import fl.managers.StyleManager;
import flash.media.*;
import fl.controls.List;
import fl.data.DataProvider;
import fl.controls.TextArea;
public class videoChat extends Sprite {
public var IP:String;
public var fmsPath:String;
public var myNC:NetConnection;
public var myNS:NetStream;
private var mySubNS:NetStream;
public var userName:String;
public var mySO:SharedObject;
public var peopleArray:Array;
public var tmpName:String;
public var msg:String
public var now:Date;
private var myCam:Camera;
private var myMic:Microphone;
public function videoChat() {
init();
startConnect();
}
public function init() {
var myTF:TextFormat=new TextFormat();
myTF.size=12;
StyleManager.setStyle("textFormat",myTF);//不理解的代码
IP="127.0.0.1";//不理解的代码
fmsPath="rtmp://"+IP+"/videoChat";//不理解的代码
userName="guest"+int(Math.random()*1000);
userName1.text=userName;
userName2.text="所有人";
now=new Date();
peopleList.addEventListener(MouseEvent.CLICK,selectChatTo);
peopleList.addEventListener(MouseEvent.DOUBLE_CLICK,getVideo);
}
public function startConnect() {
myNC=new NetConnection;
myNC.client=this;
myNC.objectEncoding=ObjectEncoding.AMF3;
myNC.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
myNC.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncError);
myNC.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityError);
myNC.connect(fmsPath,userName);
}
public function netStatus(event:NetStatusEvent):void {
switch (event.info.code) {
case "NetConnection.Connect.Success" :
chatContent.htmlText+="连接成功";
setMySO();
sendBtn.addEventListener(MouseEvent.MOUSE_DOWN,sendMsgByMouse);
stage.addEventListener(KeyboardEvent.KEY_DOWN,sendMsgByKey);
publishStream();
break;
case "NetConnection.Connect.Failed" :
chatContent.htmlText+="连接失败";
break;
case "NetConnection.Connect.Rejected" :
chatContent.htmlText+=event.info.application.message;
break;
}
}
public function securityError(event:SecurityErrorEvent):void{
chatContent.htmlText+="安全性错误"}
public function setMySO():void{
mySO=SharedObject.getRemote("mySO",myNC.uri,false)
mySO.addEventListener(SyncEvent.SYNC,mySoSync)
mySO.connect(myNC)}
public function asyncError(event:AsyncErrorEvent):void{}
public function mySoSync(e:SyncEvent):void{
peopleArray=new Array
for (var tmp in mySO.data){
peopleArray.push(tmp)}
var tmpDP:DataProvider=new DataProvider
for(var i=0;i<peopleArray.length;i++){
tmpName=peopleArray[i]
tmpDP.addItem({label:tmpName})}
tmpDP.sortOn("label")//很有可能有错
tmpDP.addItemAt({label:"所有人"},0)
peopleList.dataProvider=tmpDP
}
public function sendMsgByKey(event:KeyboardEvent):void{
if(event.keyCode==13){
sendMsg()}}
public function sendMsgByMouse(event:MouseEvent):void{
sendMsg()}
public function sendMsg():void{
if(willSendMsg.length>0){
if(userName2.text==userName1.text){
chatContent.htmlText+="你不能对你自己发言"+"\n"
chatContent.verticalScrollPosition=chatContent.maxVerticalScrollPosition}
else {
var allpeople:Array=new Array
for(var i=0;i<peopleList.length;i++){
if(peopleList.getItemAt(i).label!="所有人"&&peopleList.getItemAt(i).label!=userName1.text){
allpeople.push(peopleList.getItemAt(i).label)}}
msg="<font color='#ff0000'>"+userName1.text+"</font)"+"对"+"<font color='#ff0000'>"+userName2.text+"</font>"+"说"+"("+"<font color='#0000ff'>"+now.getHours()+":"+"</font>"+"\n"+willSendMsg.text+"\n"
if(userName2.text==null)
{
userName2.text="所有人"
}
myNC.call("msgFromClient",null,userName1.text,userName2.text,msg,allpeople)
chatContent.htmlText+=msg
chatContent.verticalScrollPosition=chatContent.maxVerticalScrollPosition
willSendMsg.text=""
}}}
public function msgFromSrvr(msg){
trace(msg)
chatContent.htmlText+=msg
chatContent.verticalScrollPosition=chatContent.maxVerticalScrollPosition}
private function selectChatTo(event:MouseEvent):void{
userName2.text=peopleList.selectedItem.label}
private function publishStream():void{
myCam=Camera.getCamera()
myMic=Microphone.getMicrophone()
liveVideo.attachCamera(myCam)
myNS=new NetStream(myNC)
myNS.client=this
myNS.addEventListener(NetStatusEvent.NET_STATUS,netStatus)
myNS.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncError)
myNS.attachAudio(myMic)
myNS.attachCamera(myCam)
myNS.publish(userName1.text,"live")
}
private function getVideo(event:MouseEvent):void{
if(userName2.text!=userName1.text){
whoseVideo.text=userName2.text+"的视频 "
mySubNS=new NetStream(myNC)
mySubNS.client=this
mySubNS.addEventListener(NetStatusEvent.NET_STATUS,netStatus)
mySubNS.addEventListener(AsyncErrorEvent.ASYNC_ERROR,asyncError)//这个函数不是很清楚
liveVideo.attachNetStream(mySubNS)
mySubNS.play(userName2.text)}
else{
mySubNS.close()
liveVideo.clear()
whoseVideo.text="我的视频"
liveVideo.attachCamera(myCam)}}
}
}
Myself.as
package{
import flash.display.Sprite
import flash.events.MouseEvent
import flash.net.ObjectEncoding
import flash.net.NetConnection
import flash.net.NetStream
import flash.text.TextField
import fl.controls.List
public class extends Myself extends Sprite{
private var myNC:NetConnection
private var myNS:NetStream
private var userName1:String
private var userName2:String
public function Myself(){
init()
started()}
public function init(){
}}}
main.asc
application.onAppStart=function(){
trace("程序已启动")
list=new Object()
application.mySO=SharedObject.get("mySO",false)}
application.onConnect=function(newClient,userName){
trace("有新的用户连接:"+userName)
if(list[userName]==null){
list[userName]=newClient
newClient.userName=userName
this.acceptConnection(newClient)
application.mySO.setProperty(newClient.userName,userName)
newClient.msgFromClient=function(user1,user2,msg,allpeople){for(var i=0;i<allpeople.length;i++){
application.list[allpeople[i]].call("msgFromSrvr",null,msg)}}}
else {
var err=new Object()
err.message="对不起,该用户已存在"
application.rejectConnection(newClient,err)}}
application.onDisconnect=function(oldUser){
application.mySO.setProperty(oldUser.userName,null)
list[oldUser.userName]=null
delete list[oldUser.userName]}