I'm playing around with RedBox's VideoConference Example. I got it thusfar working. If I click 'join conference', others can see me, and I can see others. So far, so good.
Personally, I would prefer to see others BEFORE clicking 'join conference'. Then the user can decide on it's own if it wants to be on cam or not, but it will be able to see others. A bit of the TinyChat idea .
How can I get this to work? This is my current code:
Code: Select all
/*
* SmartFoxServer 2X + RedBox Examples - VideoConference
* http://www.smartfoxserver.com
* (c) 2011 gotoAndPlay
*/
import com.smartfoxserver.v2.SmartFox;
import com.smartfoxserver.v2.core.SFSEvent;
import com.smartfoxserver.v2.redbox.AVCastManager;
import com.smartfoxserver.v2.redbox.data.LiveCast;
import com.smartfoxserver.v2.redbox.events.RedBoxCastEvent;
import com.smartfoxserver.v2.redbox.events.RedBoxConnectionEvent;
import com.smartfoxserver.v2.requests.CreateRoomRequest;
import com.smartfoxserver.v2.requests.RoomSettings;
import com.smartfoxserver.v2.util.ClientDisconnectionReason;
import components.CreateRoomPanel;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.managers.PopUpManager;
private var sfs:SmartFox;
private var alert:Alert;
private var createRoomPanel:CreateRoomPanel;
private var avCastMan:AVCastManager;
private function init():void
{
// Get reference to SmartFoxServer connection
sfs = loginPanel.connector.connection;
sfs.addEventListener(SFSEvent.LOGIN, onLogin);
sfs.addEventListener(SFSEvent.ROOM_JOIN, onRoomJoin);
sfs.addEventListener(SFSEvent.CONNECTION_LOST, onConnectionLost);
sfs.addEventListener(SFSEvent.ROOM_CREATION_ERROR, onRoomCreationError);
}
//---------------------------------
// User interaction event handlers
//---------------------------------
/**
* Create room button click event listener.
* Show a popup panel where user can set the room properties.
*/
private function onCreateRoomApplicationBtClick():void
{
createRoomPanel = PopUpManager.createPopUp(this, CreateRoomPanel, true) as CreateRoomPanel;
PopUpManager.centerPopUp(createRoomPanel);
createRoomPanel.addEventListener(CloseEvent.CLOSE, onPopUpClosed, false, 0, true);
}
/**
* Create room button click event listener.
* Create a new room using the parameters entered in the create room pupup.
*/
public function onCreateRoomPopuUpBtClick():void
{
if (createRoomPanel.ti_roomName.length > 0)
{
// Collect room settings
var settings:RoomSettings = new RoomSettings(createRoomPanel.ti_roomName.text);
settings.groupId = createRoomPanel.lb_group.text;
settings.maxUsers = createRoomPanel.ns_maxUsers.value;
// Create room
var request:CreateRoomRequest = new CreateRoomRequest(settings, true, sfs.lastJoinedRoom);
sfs.send(request);
// Close popup
removePopUp();
}
}
/**
* Join video conference
*/
public function onJoinConfBtClick():void
{
// Retrieve live casts already available
for each (var liveCast:LiveCast in avCastMan.getAvailableCasts())
{
// Subscribe live cast and add to video container
addLiveCast(liveCast);
}
// Publish my live cast
try
{
var myStream:NetStream = avCastMan.publishLiveCast(true, true);
if (myStream != null)
{
// Attach camera output
myVCItem.attachCamera(Camera.getCamera());
bt_joinConf.enabled = false;
bt_leaveConf.enabled = true;
}
}
catch (e:Error)
{
var error:String = "The following error occurred while trying to subscribe a live cast: " + e.message;
// Show alert
showAlert(error);
}
}
/**
* Leave video conference
*/
public function onLeaveConfBtClick():void
{
// Stop receiveing cast publish/unpublish notification
avCastMan.stopPublishNotification();
// Retrieve live casts
for each (var liveCast:LiveCast in avCastMan.getAvailableCasts())
{
// Unsubscribe cast
avCastMan.unsubscribeLiveCast(liveCast.id);
}
// Unpublish my live cast
avCastMan.unpublishLiveCast();
// Reset video conference container
resetVideoConference();
}
private function onAlertClosed(evt:Event):void
{
removeAlert();
}
private function onPopUpClosed(evt:Event):void
{
removePopUp();
}
//---------------------------------
// SmartFoxServer event handlers
//---------------------------------
/**
* On login, show the chat view.
*/
private function onLogin(evt:SFSEvent):void
{
// Move to chat view, and display user name
mainView.selectedChild = view_chat;
lb_myUserName.text = sfs.mySelf.name;
// Initialize AVCastManager
initializeAV();
}
/**
* When leaving a room to join a new one, the currnt user also leaves the previous video conference (if any).
* For this reason we have to reset the join/leave conference buttons and stop the camera capture.
*/
public function onRoomJoin(evt:SFSEvent):void
{
resetVideoConference();
}
/**
* On connection lost, go back to login panel view and display disconnection error message.
*/
private function onConnectionLost(evt:SFSEvent):void
{
// Remove popup, if any
removePopUp();
// Remove alert, if displayed
removeAlert();
// Remove listeners added to AVCastManager instance
if (avCastMan != null)
{
avCastMan.removeEventListener(RedBoxConnectionEvent.AV_CONNECTION_INITIALIZED, onAVConnectionInited);
avCastMan.removeEventListener(RedBoxConnectionEvent.AV_CONNECTION_ERROR, onAVConnectionError);
avCastMan.removeEventListener(RedBoxCastEvent.LIVE_CAST_PUBLISHED, onLiveCastPublished);
avCastMan.removeEventListener(RedBoxCastEvent.LIVE_CAST_UNPUBLISHED, onLiveCastUnpublished);
avCastMan = null;
}
// Show disconnection message, unless user chose voluntarily to close the connection
if (evt.params.reason != ClientDisconnectionReason.MANUAL)
{
var msg:String = "Connection lost";
switch (evt.params.reason)
{
case ClientDisconnectionReason.IDLE:
msg += "\nYou have exceeded the maximum user idle time";
break;
case ClientDisconnectionReason.KICK:
msg += "\nYou have been kicked";
break;
case ClientDisconnectionReason.BAN:
msg += "\nYou have been banned";
break;
case ClientDisconnectionReason.UNKNOWN:
msg += " due to unknown reason\nPlease check the server-side log";
break;
}
loginPanel.ta_error.text = msg;
}
// Show login view
mainView.selectedChild = view_connecting;
}
/**
* On room creation error, show an alert.
*/
private function onRoomCreationError(evt:SFSEvent):void
{
// Show alert
showAlert("Unable to create room due to the following error: " + evt.params.errorMessage);
}
//---------------------------------------------------------------------
// RedBox AVCastManager event handlers
//---------------------------------------------------------------------
/**
* Handle A/V connection initialized.
*/
public function onAVConnectionInited(evt:RedBoxConnectionEvent):void
{
// Nothing to do. Usually we should wait this event before enabling the a/v chat related interface elements.
}
/**
* Handle A/V connection error.
*/
public function onAVConnectionError(evt:RedBoxConnectionEvent):void
{
var error:String = "The following error occurred while trying to establish an A/V connection: " + evt.params.errorCode;
// Show alert
showAlert(error);
}
/**
* Handle new live cast published by user
*/
public function onLiveCastPublished(evt:RedBoxCastEvent):void
{
var liveCast:LiveCast = evt.params.liveCast;
trace ("User " + liveCast.username + " published his live cast");
// Subscribe live cast and add to video container
addLiveCast(liveCast);
}
/**
* Handle live cast unpublished by user
*/
public function onLiveCastUnpublished(evt:RedBoxCastEvent):void
{
var liveCast:LiveCast = evt.params.liveCast;
trace ("User " + liveCast.username + " unpublished his live cast");
// When a user unpublishes his live cast, the AVCastManager instance automatically unsubscribes
// that cast for the current user, so we just have to remove his video from the stage
// Remove item from video container
videoContainer.removeChild(videoContainer.getChildByName("user_" + liveCast.userId));
}
//---------------------------------
// Other methods
//---------------------------------
/**
* Initialize the AVChatManager instance.
*/
private function initializeAV():void
{
// Create AVChatmanager instance
avCastMan = new AVCastManager(sfs, sfs.currentIp, false, true);
avCastMan.addEventListener(RedBoxConnectionEvent.AV_CONNECTION_INITIALIZED, onAVConnectionInited);
avCastMan.addEventListener(RedBoxConnectionEvent.AV_CONNECTION_ERROR, onAVConnectionError);
avCastMan.addEventListener(RedBoxCastEvent.LIVE_CAST_PUBLISHED, onLiveCastPublished);
avCastMan.addEventListener(RedBoxCastEvent.LIVE_CAST_UNPUBLISHED, onLiveCastUnpublished);
}
/**
* Add live cast to video container
*/
private function addLiveCast(liveCast:LiveCast):void
{
// Subscribe cast
var stream:NetStream = avCastMan.subscribeLiveCast(liveCast.id);
if (stream != null)
{
// Add item to video container
var item:VideoConferenceItem = new VideoConferenceItem();
item.name = "user_" + liveCast.userId;
videoContainer.addChild(item);
// Attach stream to item
item.setLabelText(liveCast.username);
item.attachStream(stream);
}
}
/**
* Reset video conference container
*/
public function resetVideoConference():void
{
// Retrieve live casts
for each (var liveCast:LiveCast in avCastMan.getAvailableCasts())
{
// Remove item from video container
videoContainer.removeChild(videoContainer.getChildByName("user_" + liveCast.userId));
}
// Stop camera output
myVCItem.reset(sfs.mySelf.name + " (me)");
bt_joinConf.enabled = true;
bt_leaveConf.enabled = false;
}
private function showAlert(message:String):void
{
// Remove previous alert, if any
removeAlert();
// Show alert
alert = Alert.show(message, "Warning", Alert.OK, null, onAlertClosed);
}
private function removeAlert():void
{
if (alert != null)
PopUpManager.removePopUp(alert);
alert = null;
}
private function removePopUp():void
{
if (createRoomPanel != null)
{
createRoomPanel.removeEventListener(CloseEvent.CLOSE, onPopUpClosed);
PopUpManager.removePopUp(createRoomPanel);
createRoomPanel = null;
}
}
I already tried a lot of things, like adding GetAvailablecasts after the login, but that didn't work either.
Thanks in advance,
Riccardo