here is a code taken from Your sizing tutorial is converted from flex to flash:
- Code: Select all
package
{
import com.tokbox.TB;
import com.tokbox.events.DevicePanelEvent;
import com.tokbox.events.ExceptionEvent;
import com.tokbox.events.PublisherEvent;
import com.tokbox.events.SessionConnectEvent;
import com.tokbox.events.SessionDisconnectEvent;
import com.tokbox.events.StreamEvent;
import com.tokbox.model.DeviceManager;
import com.tokbox.model.DevicePanel;
import com.tokbox.model.Publisher;
import com.tokbox.model.PublisherProperties;
import com.tokbox.model.Session;
import com.tokbox.model.Stream;
import com.tokbox.model.Subscriber;
import com.tokbox.model.SubscriberProperties;
import flash.display.Sprite;
import flash.display.LoaderInfo;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.core.UIComponent;
public class Sizing extends Sprite
{
private static const API_KEY:String = "xxx"; // Replace with your API key.
private var SESSION_ID:String = "xxx"; // Replace with your own session ID. See http://www.tokbox.com/opentok/api/tools/generator
private var TOKEN:String = "xxx"; // Replace with a generated token. See http://www.tokbox.com/opentok/api/tools/generator
private static const PUBLISHER_WIDTH:Number = 220;
private static const PUBLISHER_HEIGHT:Number = 165;
private static const subscriber_width:Array = new Array(120, 160, 220);
private static const subscriber_height:Array = new Array(90, 120, 165);
private static const CONNECTING:String = "Connecting...";
private static const CONNECTED:String = "Connected. You can start publishing.";
private static const PUBLISHING:String = "Publishing.";
private static const UNPUBLISHING:String = "Unpublishing.";
private var _session:Session;
private var _publisher:Publisher;
private var _devicePanel:DevicePanel;
private var _callStatus:String = CONNECTING;
private var _publishing:Boolean = false;
public function Sizing()
{
init()
}
public function init():void
{
/*TOKEN = LoaderInfo(this.root.loaderInfo).parameters.token;*/
TOKEN = unescape(TOKEN);
_session = TB.initSession(SESSION_ID);
// Add event listeners to the session
_session.addEventListener(SessionConnectEvent.CONNECTED, sessionConnectedHandler);
_session.addEventListener(StreamEvent.CREATED, streamCreatedHandler);
_session.addEventListener(StreamEvent.DESTROYED, streamDestroyedHandler);
/*
If testing the app from the desktop, be sure to check the Flash Player Global Security setting
to allow the page from communicating with SWF content loaded from the web. For more information,
see http://www.tokbox.com/opentok/build/tutorials/helloworld.html#localTest
*/
_session.connect(API_KEY, TOKEN);
// Un-comment either of the following to set automatic logging and exception handling.
// See the exceptionHandler() method below.
// TB.setLogLevel(TB.DEBUG);
// TB.addEventListener("exception", exceptionHandler);
}
//--------------------------------------
// OPENTOK EVENT HANDLERS
//--------------------------------------
private function sessionConnectedHandler(event:SessionConnectEvent):void
{
// Now possible to start streaming
_callStatus = CONNECTED;
startPublishing();
}
private function streamCreatedHandler(event:StreamEvent):void
{
// When we get a streamCreated event for the stream we publish,
// build the grid of subscribers at the appropriate sizes.
for (var i:int = 0; i < event.streams.length; i++)
{
if (event.streams[i].connection.connectionId == event.target.connection.connectionId)
{
// Our publisher just started streaming
// Create sized grid of subscribers
for (var j:int = 0; j < 3; j++)
{
//var gridRow:GridRow = videoGrid.getElementAt(j + 1) as GridRow;
//gridRow.height = subscriber_height[j];
for (var k:int = 0; k < 3; k++)
{
var subscriberProps:SubscriberProperties = new SubscriberProperties();
subscriberProps.subscribeToAudio = false;
var session:Session = event.target as Session;
var subscriber:Subscriber = session.subscribe(event.streams[i], subscriberProps);
subscriber.width = subscriber_width[k];
subscriber.height = subscriber_height[j];
subscriber.x = subscriber_width[k] * j;
subscriber.y = subscriber_height[j] * k;
trace("S")
addChild(subscriber);
}
}
// Update status and controls
_callStatus = PUBLISHING;
_publishing = true;
}
}
}
private function streamDestroyedHandler(event:StreamEvent):void
{
for (var i:int = 0; i < event.streams.length; i++)
{
var session:Session = event.target as Session;
if (event.streams[i].connection.connectionId == session.connection.connectionId)
{
// Our publisher just stopped streaming
// Update status and controls
_callStatus = CONNECTED;
_publishing = false;
}
}
}
/*
* If you un-comment the call to TB.addEventListener(), above, OpenTok
* calls the exceptionHandler() method when exception events occur.
* You can modify this method to further process exception events.
* If you un-comment the call to TB.setLogLevel(), above, OpenTok
* automatically displays exception event messages.
*/
private function exceptionHandler(event:ExceptionEvent):void
{
Alert.show("Exception: " + event.code + "::" + event.message);
}
//--------------------------------------
// LINK CLICK HANDLERS
//--------------------------------------
/*private function publishButtonClickHandler():void
{
if (_publishing)
{
stopPublishing();
}
else
{
startPublishing();
}
}*/
//--------------------------------------
// PRIVATE METHODS
//--------------------------------------
// Called when user wants to start participating in the call
private function startPublishing():void
{
// Starts publishing user local camera and mic
// as a stream into the session
var publisherProps:PublisherProperties = new PublisherProperties();
publisherProps.publishAudio = false;
_publisher = TB.initPublisher(API_KEY, publisherProps);
_session.publish(_publisher);
_publisher.width = PUBLISHER_WIDTH;
_publisher.height = PUBLISHER_HEIGHT;
_publisher.x = 500
addChild(_publisher);
//_publisher.addEventListener(PublisherEvent.SETTINGS_BUTTON_CLICK, settingsButtonClickHandler, false, 0, true);
_callStatus = PUBLISHING;
//publishButton.enabled = false;
}
// Called when user wants to stop participating in the call
private function stopPublishing():void
{
if (_publishing)
{
// Stop the stream
_session.unpublish(_publisher);
_publisher = null;
}
_callStatus = UNPUBLISHING;
_publishing = false;
}
}
}
the code above works it displays me 9 subscribers
when I change the size of the subscriber window from:
- Code: Select all
private static const subscriber_width:Array = new Array(120, 160, 220);
private static const subscriber_height:Array = new Array(90, 120, 165);
to
- Code: Select all
private static const subscriber_width:Array = new Array(120, 120, 120);
private static const subscriber_height:Array = new Array(90, 90, 90);
so the overall code look s like this:
- Code: Select all
package
{
import com.tokbox.TB;
import com.tokbox.events.DevicePanelEvent;
import com.tokbox.events.ExceptionEvent;
import com.tokbox.events.PublisherEvent;
import com.tokbox.events.SessionConnectEvent;
import com.tokbox.events.SessionDisconnectEvent;
import com.tokbox.events.StreamEvent;
import com.tokbox.model.DeviceManager;
import com.tokbox.model.DevicePanel;
import com.tokbox.model.Publisher;
import com.tokbox.model.PublisherProperties;
import com.tokbox.model.Session;
import com.tokbox.model.Stream;
import com.tokbox.model.Subscriber;
import com.tokbox.model.SubscriberProperties;
import flash.display.Sprite;
import flash.display.LoaderInfo;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.core.UIComponent;
public class Sizing extends Sprite
{
private static const API_KEY:String = "xxx"; // Replace with your API key.
private var SESSION_ID:String = "xxx"; // Replace with your own session ID. See http://www.tokbox.com/opentok/api/tools/generator
private var TOKEN:String = "xxx"; // Replace with a generated token. See http://www.tokbox.com/opentok/api/tools/generator
private static const PUBLISHER_WIDTH:Number = 220;
private static const PUBLISHER_HEIGHT:Number = 165;
private static const subscriber_width:Array = new Array(120, 120, 120);
private static const subscriber_height:Array = new Array(90, 90, 90);
private static const CONNECTING:String = "Connecting...";
private static const CONNECTED:String = "Connected. You can start publishing.";
private static const PUBLISHING:String = "Publishing.";
private static const UNPUBLISHING:String = "Unpublishing.";
private var _session:Session;
private var _publisher:Publisher;
private var _devicePanel:DevicePanel;
private var _callStatus:String = CONNECTING;
private var _publishing:Boolean = false;
public function Sizing()
{
init()
}
public function init():void
{
/*TOKEN = LoaderInfo(this.root.loaderInfo).parameters.token;*/
TOKEN = unescape(TOKEN);
_session = TB.initSession(SESSION_ID);
// Add event listeners to the session
_session.addEventListener(SessionConnectEvent.CONNECTED, sessionConnectedHandler);
_session.addEventListener(StreamEvent.CREATED, streamCreatedHandler);
_session.addEventListener(StreamEvent.DESTROYED, streamDestroyedHandler);
/*
If testing the app from the desktop, be sure to check the Flash Player Global Security setting
to allow the page from communicating with SWF content loaded from the web. For more information,
see http://www.tokbox.com/opentok/build/tutorials/helloworld.html#localTest
*/
_session.connect(API_KEY, TOKEN);
// Un-comment either of the following to set automatic logging and exception handling.
// See the exceptionHandler() method below.
// TB.setLogLevel(TB.DEBUG);
// TB.addEventListener("exception", exceptionHandler);
}
//--------------------------------------
// OPENTOK EVENT HANDLERS
//--------------------------------------
private function sessionConnectedHandler(event:SessionConnectEvent):void
{
// Now possible to start streaming
_callStatus = CONNECTED;
startPublishing();
}
private function streamCreatedHandler(event:StreamEvent):void
{
// When we get a streamCreated event for the stream we publish,
// build the grid of subscribers at the appropriate sizes.
for (var i:int = 0; i < event.streams.length; i++)
{
if (event.streams[i].connection.connectionId == event.target.connection.connectionId)
{
// Our publisher just started streaming
// Create sized grid of subscribers
for (var j:int = 0; j < 3; j++)
{
//var gridRow:GridRow = videoGrid.getElementAt(j + 1) as GridRow;
//gridRow.height = subscriber_height[j];
for (var k:int = 0; k < 3; k++)
{
var subscriberProps:SubscriberProperties = new SubscriberProperties();
subscriberProps.subscribeToAudio = false;
var session:Session = event.target as Session;
var subscriber:Subscriber = session.subscribe(event.streams[i], subscriberProps);
subscriber.width = subscriber_width[k];
subscriber.height = subscriber_height[j];
subscriber.x = subscriber_width[k] * j;
subscriber.y = subscriber_height[j] * k;
trace("S")
addChild(subscriber);
}
}
// Update status and controls
_callStatus = PUBLISHING;
_publishing = true;
}
}
}
private function streamDestroyedHandler(event:StreamEvent):void
{
for (var i:int = 0; i < event.streams.length; i++)
{
var session:Session = event.target as Session;
if (event.streams[i].connection.connectionId == session.connection.connectionId)
{
// Our publisher just stopped streaming
// Update status and controls
_callStatus = CONNECTED;
_publishing = false;
}
}
}
/*
* If you un-comment the call to TB.addEventListener(), above, OpenTok
* calls the exceptionHandler() method when exception events occur.
* You can modify this method to further process exception events.
* If you un-comment the call to TB.setLogLevel(), above, OpenTok
* automatically displays exception event messages.
*/
private function exceptionHandler(event:ExceptionEvent):void
{
Alert.show("Exception: " + event.code + "::" + event.message);
}
//--------------------------------------
// LINK CLICK HANDLERS
//--------------------------------------
/*private function publishButtonClickHandler():void
{
if (_publishing)
{
stopPublishing();
}
else
{
startPublishing();
}
}*/
//--------------------------------------
// PRIVATE METHODS
//--------------------------------------
// Called when user wants to start participating in the call
private function startPublishing():void
{
// Starts publishing user local camera and mic
// as a stream into the session
var publisherProps:PublisherProperties = new PublisherProperties();
publisherProps.publishAudio = false;
_publisher = TB.initPublisher(API_KEY, publisherProps);
_session.publish(_publisher);
_publisher.width = PUBLISHER_WIDTH;
_publisher.height = PUBLISHER_HEIGHT;
_publisher.x = 500
addChild(_publisher);
//_publisher.addEventListener(PublisherEvent.SETTINGS_BUTTON_CLICK, settingsButtonClickHandler, false, 0, true);
_callStatus = PUBLISHING;
//publishButton.enabled = false;
}
// Called when user wants to stop participating in the call
private function stopPublishing():void
{
if (_publishing)
{
// Stop the stream
_session.unpublish(_publisher);
_publisher = null;
}
_callStatus = UNPUBLISHING;
_publishing = false;
}
}
}
it hands flash and tells me that the script is working for more then 15s.
http://screencast.com/t/e6PHeQKVtzwhat does LayoutMenager.validateDisplayList do ?
I guess is that it checks if the subscriber displayObject is placed somewhere, and it gets some false value and tries to position it somewhere else so it causes an infinite loop.
Why the script behaves so different if subscribers have all the exact same size?
Any idea?