# Some Help with Code please?

**URL:** <https://forum.kirupa.com/t/some-help-with-code-please/211193>\
**Category:** flash\
**Created:** [December 28, 2006, 12:00pm UTC](https://forum.kirupa.com/t/some-help-with-code-please/211193 "2006-12-28T12:00:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![brionace](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/brionace/32/3130_2.png) [@brionace](https://forum.kirupa.com/u/brionace)\
**Post date:** [December 28, 2006, 12:00pm UTC](https://forum.kirupa.com/t/some-help-with-code-please/211193/1 "2006-12-28T12:00:11Z")

</div>

Hi, Basicly i hope someone can help me with a small issue. Im new to flash.

I found code which uses a switchboard to switch between a broadcast to the swf player or client side.

I want to to have a single connection button(at broadcaster side) that will cut the switcher and enable the broadcaster to connect directly to the client.

Im not sure what exactly to change or add inthe current mains code.  
Please help. Thanks.

(the switcher has only one button[cut\_btn] that switches a stream in embedded video [prev\_video] which is activated when a broadcaster USERID is selected from a datagrid[broadcasterList\_dg])  
I want a single connect button in the broadcaster which will do what the switch button does but cut out the clicking of a datagrid and previewing basicly cut off the switcher altogether.

CODE\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

--------------------------------------------------------CODE FOR SWITCHER.FLA------------------------------------------------------------------------

/\* ----------------------------------  
INITIAL VARIABLES  
----------------------------------\*/  
\_global.connString = “rtmp://localhost/fcsBroadcast/”;  
\_global.stream = new Object();

/\* ----------------------------------  
EVENT HANDLER ASSIGNMENTS  
FOR THE USER INTERFACE COMPONENTS  
----------------------------------\*/

```
// MISC CONTROLS
cut_btn.onPress = doSwitchVideo;
broadcasterList_dg.changeHandler = doLoadPreview;
//MICROPHONE LEVEL SLIDERS
prog_micLevel_slide.changeHandler = setMicrophone;
prev_micLevel_slide.changeHandler = setMicrophone;
// MUTE BUTTONS
mutePrev_ch.clickHandler = onMute;
muteProg_ch.clickHandler = onMute;
// CAMERA QUALITY SLIDERS
prev_camQuality_slide.changeHandler = setCamera;
prog_camQuality_slide.changeHandler = setCamera;

```

/\* ----------------------------------  
NETCONNECTION STARTUP  
&nbsp;(NOTE: THIS WOULD USUALLY BE PLACED WITHIN A FUNCTION CALL)  
----------------------------------\*/  
nc = new NetConnection();  
nc.onStatus= ncStatus;  
nc.connect(connString, “SWITCHER”);

function ncStatus(nsObj) {  
trace("NC\> " +nsObj.code);  
switch(nsObj.code) {  
case “NetConnection.Connect.Success”:  
doInitStreams();  
doInitSO();  
break;  
}  
}

/\* ----------------------------------  
INITIALIZE THE STREAMS  
START PLAYING PROGRAM AND PREVUEW (NULL)  
----------------------------------\*/

function doInitStreams() {  
trace(“NS\> Initializing Streams”);  
program\_ns = new NetStream(nc);  
preview\_ns = new NetStream(nc);

```
program_ns.setBufferTime(2);
preview_ns.setBufferTime(2);    

prog_video.attachVideo(program_ns);
prev_video.attachVideo(preview_ns);

program_ns.play("programStream",-1,-1,true);
preview_ns.play("null",-1,-1,true);

setInterval(monStreams,250);

}

```

/\* ----------------------------------  
SHARED OBJECT INITIALIZATION  
AND ONSYNC HANDLERS  
----------------------------------\*/

function doInitSO() {  
trace(“SO\> Initializing SharedObjects”);  
broadcaster\_so = SharedObject.getRemote(“broadcaster”,nc.uri,false);  
broadcaster\_so.onSync = syncBroadcaster;  
broadcaster\_so.connect(nc);

```
public_so = SharedObject.getRemote("public",nc.uri,false);
public_so.onSync = syncPublic;
public_so.connect(nc);

}

```

// onSync Handler for the Broadcaster SharedObject  
function syncBroadcaster(syncObj) {  
// initialize the temporary Variables  
var soData = broadcaster\_so.data;  
var gridObj;  
var currentPublishers = new Array();

```
// Update the DataGrid with all publishers
for (i in soData) {
    // EXTRA: This line will add an asterix to the current live stream
    if (soData*.status == "LIVE") livePrefix = "* "; else livePrefix = "";
    gridObj = new Object();
    gridObj.userID=soData*.userID;
    gridObj.streamName=soData*.streamName;
    gridObj.broadcasterName=livePrefix+soData*.broadcasterName;    

    currentPublishers.push(gridObj);
}
// assign the array of objects to the Data Grid
broadcasterList_dg.dataProvider = currentPublishers;
// EXTRA: set the size of the data grid columns
broadcasterList_dg.getColumnAt(0).width = 70;
broadcasterList_dg.getColumnAt(2).width = 0;

}

```

// onSync Handler for the PUBLIC SharedObject  
function syncPublic(syncObj) {  
prog\_BroadcasterName\_txt.text = this.data.currentStream.broadcasterName;  
}

/\* ----------------------------------  
REMOTE CONTROL FUNCTIONS  
AND EVENT HANDLER ASSIGNMENTS  
----------------------------------\*/  
// REMOTE CAMERA QUALITY CONTROLS  
function setCamera() {  
var targetUserID = stream.preview.userID;  
if (this.\_name == “prog\_camQuality\_slide”) targetUserID = stream.program.userID;

```
broadcaster_so.send("onCameraSet",targetUserID, this.value)
trace("Setting Camera: " + targetUserID);

}

```

// REMOTE MICROPHONE GAIN CONTROLS  
function setMicrophone() {  
var targetUserID = stream.preview.userID;  
if (this.\_name == “prog\_micLevel\_slide”) targetUserID = stream.program.userID;

```
broadcaster_so.send("onMicSet",targetUserID, this.value)
trace("Setting Microphone: " + targetUserID);

}

```

// LOCAL AUDIO CONTROL  
function onMute() {  
var targetStream = “program\_ns”;  
if (this.\_name == “mutePrev\_ch”) targetStream = “preview\_ns”;  
trace(targetStream);  
this.\_parent[targetStream].receiveAudio(this.value);

```
}

```

/\* ----------------------------------  
&nbsp;PREVIEW WINDOW AND SWITCHER FUNCTIONALITY  
----------------------------------\*/

// CONTROL THE PREVIEW WINDOW  
function doLoadPreview() {  
\_global.stream.preview = broadcasterList\_dg.selectedItem;

```
prev_BroadcasterName_txt.text = stream.preview.broadcasterName;

preview_ns.play(stream.preview.streamName);    

}

```

// SWITCH THE PROGRAM STREAM  
function doSwitchVideo() {

```
_global.stream.program = _global.stream.preview;
_global.stream.preview = public_so.data.currentStream;

preview_ns.play(stream.preview.streamName);
prev_BroadcasterName_txt.text = stream.preview.broadcasterName;

trace("SWITCHING TO: " + stream.program.userID);

// send the User ID to the server for switching
nc.call("switchStream",null,stream.program.userID);

}    

```

------------------------------------------------------------------------------CODE FOR BROADCASTER---------------------------------------------------

/\* ----------------------------------  
INITIAL VARIABLES  
----------------------------------\*/

\_global.connString = “rtmp://localhost/fcsBroadcast/”;

/\* ----------------------------------  
EVENT HANDLER ASSIGNMENTS  
FOR THE USER INTERFACE COMPONENTS  
----------------------------------\*/

```
camQuality_slide.changeHandler = doSetCamera;
micLevel_slide.changeHandler = doSetMicLevel;
previewMode_cb.changeHandler = previewMode;
setName_btn.clickHandler = broadcasterName_txt.onKillFocus = updateSystem;

```

/\* ----------------------------------  
NETCONNECTION STARTUP  
&nbsp;(NOTE: THIS WOULD USUALLY BE PLACED WITHIN A FUNCTION CALL)  
----------------------------------\*/  
nc = new NetConnection();  
nc.onStatus= ncStatus;  
nc.connect(connString, “BROADCASTER”);

function ncStatus(nsObj) {  
trace("NC\> " +nsObj.code);  
switch(nsObj.code) {  
case “NetConnection.Connect.Success”:  
onMessage("FCS Server OK\> " + this.uri);  
initDevices();  
doInitSO();  
this.call(“getClientObj”, new onFCSData());  
break;  
}  
}

// Handle the call back from the server. Returning the Client Object  
function onFCSData() {  
// EVENT HANDLER (NOTE, YOU CAN ALSO PLACE AN onStatus Handler to handle Errors  
this.onResult = function(clientObj) {  
\_global.clientObj = clientObj;  
initStreams(clientObj.streamName);

```
    }
}

```

/\* ----------------------------------  
INITIALIZE THE OUTGOING STREAMS  
----------------------------------\*/  
function initStreams(streamName) {  
out\_ns = new NetStream(nc);  
out\_ns.attachAudio(source\_mic);  
out\_ns.attachVideo(source\_cam);  
out\_ns.publish(streamName, “live”);  
// EXTRA: the onMessage function writes message to the textArea  
onMessage("STREAM\> Publishing to Stream: " + streamName);  
}

/\* ----------------------------------  
INITIALIZE THE CAPTURE DEVICES  
----------------------------------\*/  
function initDevices() {  
source\_cam = Camera.get();

```
source_mic = Microphone.get();
source_mic.setRate(11);

doSetCamera();
doSetMicLevel();

/* attach the camera to the video UI Object */
local_video.attachVideo(source_cam);

}    

```

/\* ----------------------------------  
INITIALIZE THE SHARED OBJECTS  
----------------------------------\*/  
function doInitSO() {  
trace(“SO\> Initializing SharedObjects”);  
broadcaster\_so = SharedObject.getRemote(“broadcaster”,nc.uri,false);  
broadcaster\_so.onSync = syncBroadcaster;  
// CREATE NEW EVENT HANDLERS FOR THE REMOTE CONTROLS  
broadcaster\_so.onCameraSet = onCameraSet;  
broadcaster\_so.onMicSet = onMicSet;  
// DO THE CONNECTION  
broadcaster\_so.connect(nc);  
}

// HANDLE SYNCHRONIZATION EVENTS  
function syncBroadcaster(syncObj) {  
var mySlot = this.data[\_global.clientObj.userID];  
cameraBG\_mc.gotoAndStop(mySlot.status);  
statusMsg\_txt.text = mySlot.status;  
}

/\* ----------------------------------  
HANDLE THE REMOTE CONTROL EVENT  
TO SET THE CAMERA PROPERTIES AND  
THE MICROPHONE PROPERTIES  
----------------------------------\*/  
function onCameraSet(targetUserID, newValue) {  
if (targetUserID == clientObj.userID)  
camQuality\_slide.value = newValue;  
}

function onMicSet(targetUserID, newValue) {  
if (targetUserID == clientObj.userID)  
micLevel\_slide.value = newValue;  
}

/\* ----------------------------------  
FUNCTIONS USED TO HANDLE THE  
SLIDER COMPONENT EVENTS  
CAMERA: doSetCamera  
MICROPHONE: doSetMic  
----------------------------------\*/

// SET THE CAMERA PROPERTIES  
function doSetCamera() {  
var camSet = new Object();

```
switch(camQuality_slide.value) {
    case 1:
        camSet.w = 80;
        camSet.h = 60;
        camSet.fps = 8;
        camSet.quality = 75;
        break;
    case 2:
        camSet.w = 192;
        camSet.h = 144;
        camSet.fps = 7;
        camSet.quality = 80;
        break;            
    case 3:
        camSet.w = 320;
        camSet.h = 240;
        camSet.fps = 15;
        camSet.quality = 90;
        break;            
    
    }
// calculated KeyFrame and Bandwidth
camSet.kfi = camSet.fps * 4;
camSet.bw = (camSet.w * camSet.h * camSet.fps) / 8;

// Set the Camera    
source_cam.setMode(camSet.w,camSet.h,camSet.fps,false);
source_cam.setQuality(camSet.bw,set.quality);
source_cam.setKeyFrameInterval(camSet.kfi);

onMessage("Camera Set to: " + camSet.bw + " bytes/s (" + Math.round((camSet.bw *8) / 1000) + " Kbits/s)" );
}

```

// Handle the Microphone settings  
function doSetMicLevel() {  
trace(“setting Mike Level”);  
source\_mic.setGain(micLevel\_slide.value);  
micLevel\_txt.text = source\_mic.gain;  
}

/\* ----------------------------------  
SET THE LOOPBACK FOR THE PREVIEW MODE  
----------------------------------\*/  
function previewMode() {  
var isLoopBack = previewMode\_cb.selectedItem.data;  
source\_cam.setLoopback(isLoopBack);  
}

/\* ----------------------------------  
UPDATE THE CLIENT OBJECT FOR THIS BROADCASTER  
THE SERVER WILL SEND A CALL BACK MESSAGE,  
HANDLE IT WITH onFCSUpdate  
----------------------------------\*/  
function updateSystem() {  
var deltaObj = new Object();  
deltaObj.broadcasterName = broadcasterName\_txt.text;  
nc.call(“updateBroadcaster”, new onFCSUpdate, deltaObj);  
}

function onFCSUpdate() {  
trace(“setting clientObject”);  
this.onResult = function(clientObj) {  
\_global.clientObj = clientObj;  
}  
}

-----------------------------------------------------------------------THE MAIN.ASC CODE------------------------------------------------------------------

application.onAppStart = function() {  
trace(“starting up”);

```
// Set initial Values
userID = 0;
application.currentStreamID = undefined;
/* EXTRA: THESE LINES WILL ADD PRE-RECORDED VIDEO TO THE DATAGRID
           &nbsp;PLACE THE VIDEO IN THE FOLDER streams/_definst_
*/
    preRecordedFLV = new Array();
    preRecordedFLV.push({broadcasterName: "Cujo Video", streamName:"cujo", userID:"flv1"});
    preRecordedFLV.push({broadcasterName: "Jeep Video", streamName:"jeep", userID:"flv2"});

// setup the BROADCASTER SharedObject
broadcaster_so = SharedObject.get("broadcaster",false);

// EXTRA: Assign the PreRecorded FLV Data
broadcaster_so.setProperty("flv1", preRecordedFLV[0]);
broadcaster_so.setProperty("flv2", preRecordedFLV[1]);

// setup the PUBLIC SharedObject    
public_so = SharedObject.get("public",false);
public_so.setProperty("currentStream", ({broadcaster: "No Broadcaster"}));

// Start the program stream (this is the one everyone will subscribe to
prog_stream = Stream.get("programStream");
prog_stream.play("null");

}

```

application.onConnect = function(clientObj,userType) {  
// increment the user counter;  
userID++

```
if(userType == "BROADCASTER") clientObj = initBroadcaster(clientObj);

// Accept All Connections
application.acceptConnection(clientObj);

}

```

application.onDisconnect = function(clientObj) {

```
    broadcaster_so.setProperty(clientObj.userID, undefined);
    broadcaster_so.flush();
    
    if(application.currentStreamID == clientObj.userID) {
            application.currentStreamID = undefined;
            public_so.setProperty("currentStream", ({broadcasterName: "Please Stand By..."}));
            }
}

```

initBroadcaster = function(clientObj) {  
trace(“initializing a Broadcaster”);

```
clientObj.streamName = "stream_" + userID;
clientObj.broadcasterName = "Broadcaster #" + userID;
clientObj.userID = userID;
clientObj.status = "READY"
  
broadcaster_so.setProperty(userID, clientObj);
    
return clientObj;
}

```

Client.prototype.switchStream = function(userID) {

```
var oldVal = new Object();
var currentStream;

    // get the PREVIOUS Stream Object
    if (application.currentStreamID != undefined){
        trace("Setting Previous Stream");
        oldVal = broadcaster_so.getProperty(application.currentStreamID);
        oldVal.status = "READY";
        broadcaster_so.setProperty(oldVal.userID, oldVal);
        }

    // get the NEW Stream Object
     currentStream = broadcaster_so.getProperty(userID);
    currentStream.status = "LIVE";

    broadcaster_so.setProperty(userID, currentStream);    
    public_so.setProperty("currentStream", currentStream);

    // set the currentStreamID to the new Stream ID
    application.currentStreamID = userID;    
    
    // connect the new stream with the programStream
    prog_stream.play(currentStream.streamName);

    trace(oldVal.userID + " - SWITCHING STREAMS - " + currentStream.userID);

}

```

Client.prototype.getClientObj = function() {  
trace(“sending Client Object”);  
return this;  
}

Client.prototype.updateBroadcaster = function(deltaObj) {  
// set the changed properties in the clientObject  
for (i in deltaObj) {  
this\* = deltaObj\*;  
}

```
// update the Broadcaster SharedObject
broadcaster_so.setProperty(this.userID, this);

// update the Public SharedObject
if (application.currentStreamID == this.userID) {
    public_so.setProperty("currentStream", this);        
    }

// return the updated Client Object back to the Flash player
return this;
}

// If server-side code is part of the application,

```

// it must define an onConnect function that accepts  
// the client connection.  
application.onConnect = function(client) {

```
 // Establish the connection

 trace('connected client'+client.ip);
 application.acceptConnection(c lient);

```

}
