Friday 22 June 2007
Monday 18 June 2007
detect whether user allowed or denied access to camera
If you want to detect whether the user allowed or denied access to the camera, you can listen for the camera's status
event (StatusEvent.STATUS
), as seen in the following code:
var cam:Camera = Camera.getCamera();
if (cam != null){
cam.addEventListener(StatusEvent.STATUS, statusHandler);
var vid:Video = new Video();
vid.attachCamera(cam);
addChild(vid);
}
function statusHandler(event:StatusEvent):void{
switch (event.code){
case "Camera.Muted":
trace("User clicked Deny.");
break;
case "Camera.Unmuted":
trace("User clicked Accept.");
break;
}
}
The event parameter of the statusHandler()
function contains a code property which contains the string "Camera.Muted"
or "Camera.Unmuted"
. If the value is "Camera.Muted" the user clicked the Deny button and Flash Player is unable to access the camera.
simple publisher / subscriber stream
// writeln( ) replaces trace( ) and writes messages into the trace_txt field.
function writeln (msg) {
trace_txt.text += msg + "\n";
trace_txt.scroll = trace_txt.maxscroll;
}
// onStatus( ) sets up the streams and buttons when a connection is
// established or lost.
NetConnection.prototype.onStatus = function (info) {
writeln("Level: " + info.level + ", code: " + info.code);
if (info.code == "NetConnection.Connect.Success") {
out_ns = new NetStream(this);
out_ns.attachAudio(Microphone.get( ));
out_ns.attachVideo(Camera.get( ));
in_ns = new NetStream(this);
in_ns.showMessage = function (msg) {
writeln(msg);
};
// remote_video is the instance name of an embedded video object.
remote_video.attachVideo(in_ns);
in_ns.play("public/" + remoteUserName);
connect_pb.setLabel("Disconnect");
send_pb.setEnabled(true);
}
else if (!this.isConnected) {
if (out_ns) out_ns.close( );
if (in_ns) in_ns.close( );
connect_pb.setLabel("Connect");
send_pb.setEnabled(false);
}
};
NetStream.prototype.onStatus = function (info) {
for (var p in info) {
writeln("info." + p + ": " + info[p]);
}
};
// doSend( ) is called when the send_pb is clicked.
function doSend (msg) {
out_ns.send("showMessage", input_txt.text);
input_txt.text = "";
}
/* doConnect( ) is called when the connect_pb is clicked.
* It stores the text in the userName_txt and remoteUserName_txt
* fields in global variables for later use if a connection is
* attempted. See the onStatus( ) method.
*/
function doConnect (pb) {
if (pb.getLabel( ) == "Connect") {
userName = userName_txt.text;
remoteUserName = remoteUserName_txt.text;
nc.connect("rtmp:/courseChat/algebra101", userName);
}
else {
nc.close( );
}
}
function doSendStream (pb) {
if (pb.getLabel( ) == "Send Stream" && nc.isConnected) {
pb.setLabel("Stop Stream");
out_ns.publish("public/" + userName);
}
else {
pb.setLabel("Send Stream");
out_ns.publish(false);
}
}
send_pb.setEnabled(false);
nc = new NetConnection( );
streaming bandwidth requirements
500Kbit/second * 60 sec/minute * 60 min/hour * 24hrs * 30 days = 1296Gbit a month
Many providers ask for high prices for exceeding Bandwidth, so beware.
Average 1 min recording with a high streaming quality amounts to about 4MB. So 1GB of web space lasts for about 250 minutes of recording. But lots can be done about the streaming amount, the challenge lying within not loosing to much noticeable video quality.
videoStream quality settings
activeCamera
.setQuality(bandwidth
,frameQuality
)
Method; sets the maximum amount of bandwidth per second or the required picture quality of the current outgoing video feed.
The following code allows you to see the effects of setQuality(). You can include it on the main timeline of a Flash file in which you have placed a Video object named content_vid and NumericStepper components named cnsQuality and cnsBandwidth on stage:
var user_cam = Camera.get( );
// Display the video in the Video object.
content_vid.attachVideo(user_cam);
// Set the loopback so that you can view the compressed video.
user_cam.setLoopback(true);
var qualityListener = new Object( );
qualityListener.change = function (event) {
user_cam.setQuality(user_cam.bandwidth, event.target.value);
};
cnsQuality.addEventListener("change", qualityListener);
cnsQuality.minimum = 0;
cnsQuality.maximum = 100;
cnsQuality.value = user_cam.quality;
var bandwidthListener = new Object( );
bandwidthListener.change = function (event) {
user_cam.setQuality(event.target.value, user_cam.quality);
};
cnsBandwidth.addEventListener("change", bandwidthListener);
cnsBandwidth.minimum = 0;
cnsBandwidth.maximum = 400000;
cnsBandwidth.stepSize = 1000;
cnsBandwidth.value = user_cam.quality;
setLoopback(true) displays the stream in the preview as compressed, and requires a lot of processor power (compiling and decompiling at the same time), and there for only recommended for development and testing purposes.
KeyFrames:By default Flash publishes a stream with a keyFrame every 15 frames. You can adjust this with:
user_cam.setKeyFrameInterval(10);
whether you set the parameter in setMode to true or false defines whether you want to give the frameRate priority or not; default settings are(160,120,15)
//(width,height,frameRate,frameRatePriority)
user_cam.setMode(320, 240, 25, true);