【轉帖】實現(xiàn)了視頻私聊功能
昨天匆忙實現(xiàn)了視頻私聊功能,今天把思路再整理下。順便和各位做個探討~
我的基本思路是這樣的:
1. 用戶登錄聊天室后在左邊的窗口自動顯示視頻(如果檢測到有的話,如果沒有則顯示相關文字或圖片,這個暫時還沒有加);
2. 點擊用戶列表中的相應用戶名后取得對應的視頻文件名,然后在右邊的窗口播放就可以了。
看起來簡單,其實不然哦~下面看詳細的分析
注意:下面所有修改都是基于FlashCom 組件,相關的組件實現(xiàn)就不再詳細敘述。只是摘取視頻私聊相關部分分析。
關于自動開啟視頻的代碼修改:
在av組件中的connect方法中添加
1 : //av1_mc 是左邊視頻窗口的實例名稱
2 : if (this.name=='av1_mc') this.startPublish();
這個比較容易實現(xiàn),關鍵是私聊用戶的視頻顯示。
因為AVPresence組件的視頻發(fā)布是this.prefix + this.soName
1 : this.ns.publish(this.prefix + this.soName);
也就是說是以窗口的實例名來定位播放的,要修改為針對用戶定位播放的方式,因此以上代碼改為
1 : this.ns.publish(this.userID);
這樣就是以用戶ID為文件名發(fā)布視頻文件,以便于我們下一步選擇不同的用戶來播放相應的視頻。
下一步,是修改接收視頻部分。
原先接收的方式是當前窗口接收,代碼為:
1 : // Starts receiving a stream from another user
2 : FCAVPresenceClass.prototype.startReceive = function() {
3 : // trace('Start Receiving');
4 : this.seat_video.attachVideo(this.ns);
5 : this.ns.play(this.prefix + this.soName);
6 : this.gotoAndStop('receiving');
7 : this.onStartReceive();
8 : };
現(xiàn)在要改為指定播放窗口接收視頻(這里就是右邊的窗口了),而且要指明接收哪個用戶的,所以加傳遞用戶ID參數(shù),代碼改為:
1 : FCAVPresenceClass.prototype.startReceive = function(userId) {
2 : if (userId !=this.userID){ //如果選擇自己,則不再接收
3 : this.seat_video.attachVideo(this.ns);
4 : this.ns.play(userId);
5 : this.gotoAndStop('receiving'); }
6 : };
接著要修改同步的部分,否則一旦視頻同步就會亂了。下面是修改后的代碼,比較下就知道了。
01 : this.so.onSync = function(list) {
02 : for (i in list) {
03 : userID[this.data.speaker]=this.data.speakerID;//把用戶ID保存到數(shù)組
04 : if (list[ i ].name == 'broadcast') {
05 : if (this.data.broadcast && this.data.speakerID!=this.userID && this.data.speakerID == this.selectUserID) {
06 : this.owner.startReceive(this.data.speakerID);
07 : this.owner.username_txt.text = this.data.speaker;
08 : } else {
09 : this.owner.stopReceive();
10 : this.owner.level.mask._height = 1;
11 : this.owner.username_txt.text = '(Open)';
12 : // Stop
13 : }
14 : }
15 : }
16 : };其中selectUserID就是我們點擊用戶列表后獲取到的值,要能夠獲取到這個值首先要在peopleList組件中保存用戶信息,在組件onchange事件觸發(fā)的時候可以獲取到用戶名。
我在用戶列表的value中保存了很多信息,下面是peopleList組件的同步時候添加List數(shù)據(jù)代碼
1 : this.owner.people_lb.addItem(this.data[ i ].name,{ id:i,sex:this.data[ i ].sex,name:this.data[ i ].name,ip:this.data[ i ].ip,cam:this.data[ i ].cam,mic:this.data[ i ].mic });
這些用戶信息是通過共享對象存儲在服務器端的
下面是服務器端people.asc文件中的相關實現(xiàn)
1 : FCPeopleList.prototype.connect = function( client,userSex,cam,mic ) {
2 : var cglobal = this.getClientGlobalStorage(client);
3 : var clocal = this.getClientLocalStorage(client);
4 : clocal.id = 'u' + this.getClientID(client);
5 : userName=cglobal.username == null ? ' fc_lurker' : cglobal.username;
6 : this.users_so.setProperty(clocal.id, { name:userName, cam:cam,mic:mic,sex:userSex,ip:client.ip });
7 : }
最后要做的就是點擊用戶列表中的名稱后顯示相應的視頻了
在onchange事件中寫入下面代碼就可以了:
1 : //av2_mc 是右邊窗口的實例名稱
2 : av2_mc.username_txt.text = this.getValue().name;
3 : av2_mc.selectUserId = userID[this.getValue().name];
4 : av2_mc.startReceive(userID[this.getValue().name]);
以上簡要說明了基于FlashCom組件實現(xiàn)私聊的方法,分析的不是很周全,難免存在些Bug~只是作些交流,本人也會不斷完善
posted on 2006-06-30 17:11
blog搬家了--[www.ialway.com/blog] 閱讀(1383)
評論(2) 編輯 收藏 所屬分類:
FMS