Unable to Connect to FMS - actionscript-3

Why cant i connect? When i run the application i receive NetConnection.failed in the output. Can you please take a carefull look at my code. I believe my string url is correct. I am unsure is my FMS is down or if theres a code error.
Thank You
import flash.net.NetConnection;
import flash.events.NetStatusEvent;
import flash.net.NetStream;
import flash.media.Camera;
import flash.media.Video;
import flash.media.Microphone;
import fl.controls.Button;
var nc:NetConnection;
var ns:NetStream;
var cam:Camera;
var microphone:Microphone;
var vid:Video;
var connectButton:Button;
//Connecting to Flash Media Server
nc = new NetConnection();
nc.connect("rtmfp:/fms/streamExample");
nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler);
function netHandler(event:NetStatusEvent):void
{
switch (event.info.code)
{
case "NetConnection.Connect.Success":
{
connectButton.label = "Disconnect";
connectButton.enabled = true;
sendButton.enabled = true;
onConnect();
break;
}
case "NetConnection.Connect.Closed":
{
connectButton.label = "Connect";
connectButton.enabled = true;
sendButton.enabled = false;
nc.close();
break;
}
case "NetConnection.Connect.Failed":
{
trace("Sorry your connection failed");
break;
}
case "NetConnection.Connect.Rejected":
{
trace("Oops the connection has been rejected");
break;
}
}
}
ns = new NetStream(nc);
ns.publish("live", "viewing");
ns.attachCamera();
//Setting the video
var vid:Video = new Video();
vid.height = cam.height;
vid.width = cam.width;
vid.x = 10;
vid.y = 10;
addChild(vid);
cam = Camera.getCamera();
cam.setMode(400, 300, 20);
cam.setQuality(0, 85);
cam.setKeyFrameInterval(18);
vid.attachCamera();
addChild(cam);
//Setting the audio
microphone = Microphone.getMicrophone();
microphone.codec = SoundCodec.SPEEX;
microphone.framesPerPacket = 1;
microphone.setSilenceLevel(0, 200);
microphone.gain = 50;
NetStream.attachAudio(microphone);

If your Flash Media Server is on the same machine than the one your testing your application from, you should change the URL of server with rtmfp://localhost/streamExample
If your server is on another machine, then you just have to replace localhost with the IP address of the machine running the server.
You should also add the event listener on your NetConnection before you perform the connect() call.
To help you known the reason of the failure, you might want to trace the description of your event as well.
Here is a sample with all these indications:
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netHandler);
nc.connect("rtmfp:/localhost/streamExample");
function netHandler(event:NetStatusEvent):void
{
switch (event.info.code)
{
case "NetConnection.Connect.Success":
{
connectButton.label = "Disconnect";
connectButton.enabled = true;
sendButton.enabled = true;
onConnect();
break;
}
case "NetConnection.Connect.Closed":
{
connectButton.label = "Connect";
connectButton.enabled = true;
sendButton.enabled = false;
nc.close();
break;
}
case "NetConnection.Connect.Failed":
{
trace("Sorry your connection failed");
trace("Reason is: " + event.info.description);
break;
}
case "NetConnection.Connect.Rejected":
{
trace("Oops the connection has been rejected");
break;
}
}
}

Related

How to use NetStream to request?

Before I use NetConnection to request is normal.now I need to use NetStream to request. but I do not know how to use it。
This is NetConnection code.
`
public function start():void
{
uploadtime = KService.ContextService.configure.uploadtime[0];
downloadtime = KService.ContextService.configure.downloadtime[0];
jittertime = KService.ContextService.configure.jittertime[0];
totaltime = uploadtime + downloadtime + jittertime;
checkPercent();
for(var i:int;i<512;i++)
{
upsendStr+="1";
}
// var url:String = CheckInit.server;//KService.ContextService.configure.server[0].#url;
var url:String = "rtmfp://10.27.11.51/MonaStatus/";//10.27.11.51 is fake
if (_conn)
_conn.close();
_conn = new NetConnection();
_conn.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
_conn.connect(url);
_conn.client = this;
var stream_ns:NetStream = new NetStream(_conn);
stream_ns.dataReliable = false;
// stream_ns.play("MyVideo");
//// vid.attachNetStream(stream_ns);
// stream_ns.client = stream_ns;
// stream_ns.send("myFunction", "hello");
}
private function onStatus(event:NetStatusEvent):void
{
trace(event.info.code);
switch(event.info.code) {
case "NetConnection.Connect.Success":
{
show("正在测试中,请耐心等待...");
btnStart.enabled = false;
checkUploadSpeed();
// checkDownLoadSpeed();
// checkLostAndLatency
// checkJitter();
break;
}
case "NetConnection.Connect.Closed":
{
connectionFailed();
break;
}
case "NetConnection.Connect.Failed":
{
connectionFailed();
break;
}
default:
{
}
}
}
private function checkUploadSpeed():void
{
upSpeed = 0;
show("正在测试上传速度...");
setTimeout(stopUploadData,uploadtime);
uid = UIDUtil.createUID();
var processcount:Number = Number(KService.ContextService.configure.processcount[0]);
for(var i:int;i<processcount;i++)
{
var upcheck:UploadCheck = new UploadCheck();
upcheck.uid = uid;
upcheck.start();
}
}
`
The role of this request is equivalent to use PING tools. Now I should how to use the NetStream to achieve the request, it is the role of testing within the specified time between the two IP delay, jitter and packet loss rate

Flash Webcam Capture

I am trying to build AS3 web audio/video capture. I have successfully tried to display the webcam stream with the help of this great tutorial. The next step is how do i capture the video when the user starts recording. Below is my code:
// video stuff
private var camW:int = 300;
private var camH:int = 300;
private var video:Video;
// block stuff
private var rows:int = 3;
private var cols:int = 3;
private var blockW:int = camW/cols;
private var blockH:int = camH/rows;
private var pointArray:Array = new Array();
public function Main():void {
// Checks if camera is installed
if (Camera.names.length > 0) {
trace("User has at least one camera installed.");
var camera:Camera = Camera.getCamera();
camera.setMode(camW, camH, 30);
camera.addEventListener(StatusEvent.STATUS, statusHandler);
video = new Video(camW, camH);
video.attachCamera(camera);
initBlocks();
addEventListener(Event.ENTER_FRAME, updateBlocks);
}else {
trace("User has no cameras installed.");
var text:TextField = new TextField();
text.text = "Device Not Found! Please check if the camera is installed.";
text.x = 20;
text.y = 20;
text.width = 500;
addChild(text);
}
function statusHandler(event:StatusEvent):void {
// This event gets dispatched when the user clicks the "Allow" or "Deny"
// button in the Flash Player Settings dialog box.
trace(event.code); // "Camera.Muted" or "Camera.Unmuted"
switch (event.code) {
case "Camera.Muted":
trace("User clicked Deny.");
var text:TextField = new TextField();
text.text = "Device Denied Permission! Please provide permission to record video!";
text.x = 20;
text.y = 20;
text.width = 500;
addChild(text);
break;
case "Camera.Unmuted":
trace("User clicked Accept.");
break;
}
}
}
private function initBlocks():void {
for (var r:int = 0; r < rows; r++) {
for (var c:int = 0; c < cols; c++) {
var newBlock:Sprite = new Sprite();
newBlock.name = "block" + r + c;
var p:Point = new Point(c * blockW, r * blockH);
newBlock.x = c * (blockW) + 20;
newBlock.y = r * (blockH) + 20;
pointArray[newBlock.name] = p;
var bmpd:BitmapData = new BitmapData(blockW, blockH);
var bmp:Bitmap = new Bitmap(bmpd);
bmp.name = "myBmp";
newBlock.addChild(bmp);
addChild(newBlock);
}
}
}
private function updateBlocks(e:Event):void {
var srcBmpd:BitmapData = new BitmapData(camW, camH);
srcBmpd.draw(video);
for (var r:int = 0; r < rows; r++) {
for (var c:int = 0; c < cols; c++) {
var b_mc:Sprite = this.getChildByName("block" + r + c) as Sprite;
var bmp:Bitmap = b_mc.getChildByName("myBmp") as Bitmap;
var p:Point = pointArray[b_mc.name];
bmp.bitmapData.copyPixels(srcBmpd, new Rectangle(p.x, p.y, blockW, blockH), new Point());
}
}
}
I am completely new to AS3, just started 2 days ago. What is the logic for audio/video capture in AS3? Any pointers/demo/tutorials/books appreciated
I had a very difficult time getting to work on the flash webcam recording. And as couple of posts here ( this one ) suggested to use some kind of flash servers like the Adobe Flash Server / Red5 Server. I did the same. I installed Red5 Server ( Please make sure you use the right JRE for the server to run smoothly after installation - as i had problems installing the demos ) and used Flex with FlashDevelop IDE ( Use Flex 3 Project to create a new project ). Here's below working sample code:
Main.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script source="Main.as"/>
<mx:VideoDisplay width="300" height="300" autoPlay="true" creationComplete="initCamera()" id="video"></mx:VideoDisplay>
<mx:Button label="Record" click="startRecording()" />
<mx:Button label="Stop" click="stopRecording()" />
</mx:Application>
Main.as
/**
* ...
* #author VishwaKumar
*/
import flash.events.NetStatusEvent;
import flash.media.Microphone;
import flash.net.NetConnection;
import flash.net.NetStream;
import mx.controls.Alert;
import flash.media.Camera;
import flash.media.Video;
import flash.display.Sprite;
public var camera:Camera;
public var nc:NetConnection;
public var ns:NetStream;
public var mic:Microphone;
public function initCamera():void {
var camW:int = 300;
var camH:int = 300;
if (Camera.names.length > 0) {
camera = Camera.getCamera();
camera.setMode(camW, camH, 30);
video.attachCamera(camera);
mic = Microphone.getMicrophone();
}else {
Alert.show("Device Not Found! Please check if the camera is installed.","Hardware Error!");
}
}
public function startRecording():void {
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc.connect("rtmp://127.0.0.1/oflaDemo");
}
public function netStatusHandler(event:NetStatusEvent):void {
trace(event.info.code);
switch(event.info.code) {
case "NetConnection.Connect.Success":
ns = new NetStream(nc);
ns.publish("test","record");
ns.attachAudio(mic);
ns.attachCamera(camera);
break;
default :
Alert.show("NetConnection.Connect.error");
break;
}
}
public function stopRecording():void {
ns.close();
}

How to stablish a Flash Peer-To-Peer Communication Over LAN (Without Cirrus/Stratus) with AIR

I'm trying to stream audio/video (I'm really only interested in Audio) in a LAN.
I've followed tutorials in internet which I've found very informative like the ones from Tom Krcha and thought would solve my problem. But until now I've not been successful in receiving the stream.
This is the code I'm using, can someone please point me out what am I missing?
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.media.Camera;
import flash.media.Microphone;
import flash.media.Video;
import flash.net.GroupSpecifier;
import flash.net.NetConnection;
import flash.net.NetGroup;
import flash.net.NetStream;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
[SWF(width="640", height="920", frameRate="60")]
public class LoudApp extends Sprite {
private var _interpreterStartButton:TextField = new TextField();
private var _listenerStartButton:TextField = new TextField();
private var _connectedLabel:TextField = new TextField();
private var _userTextField:TextField = new TextField();
private var _stream:NetStream;
private var _netConnection:NetConnection;
private var _netGroup:NetGroup;
private var _isConnected:Boolean;
private var _listenerStream:NetStream;
private var _isListener:Boolean;
private var _video:Video;
public function LoudApp() {
addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
}
private function connect():void{
_netConnection = new NetConnection();
_netConnection.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
_netConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
_netConnection.connect("rtmfp:");
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
private function init(event:Event):void {
_listenerStartButton.border = _interpreterStartButton.border = true;
_listenerStartButton.backgroundColor = _interpreterStartButton.borderColor = 0;
_listenerStartButton.autoSize = _interpreterStartButton.autoSize = TextFieldAutoSize.LEFT;
_listenerStartButton.selectable = _interpreterStartButton.selectable = false;
_connectedLabel = new TextField();
_connectedLabel.y = 70;
_connectedLabel.text = "not connected";
_listenerStartButton.addEventListener(MouseEvent.CLICK, onButtonClicked, false, 0, true);
_interpreterStartButton.addEventListener(MouseEvent.CLICK, onButtonClicked, false, 0, true);
_interpreterStartButton.text = " Start\rTalking! ";
_listenerStartButton.text = " Start\rListening! ";
addChild(_interpreterStartButton);
addChild(_listenerStartButton);
addChild(_connectedLabel);
_listenerStartButton.x = 120;
var textFormat:TextFormat = new TextFormat(null, 30, 0x000000, true);
_listenerStartButton.setTextFormat(textFormat);
_interpreterStartButton.setTextFormat(textFormat);
// Init the Video
_video = new Video(stage.stageWidth, stage.stageHeight - 100);
_video.y = 100;
addChild(_video);
connect();
}
private function onButtonClicked(event:MouseEvent):void {
_isListener = event.target == _listenerStartButton;
_listenerStartButton.removeEventListener(MouseEvent.CLICK, onButtonClicked);
_interpreterStartButton.removeEventListener(MouseEvent.CLICK, onButtonClicked);
removeChild(_listenerStartButton) && removeChild(_interpreterStartButton);
/*_isConnected && */setupStream();
}
private function setupStream():void{
var groupSpecifier:GroupSpecifier = new GroupSpecifier("en-GB");
groupSpecifier.serverChannelEnabled = true;
groupSpecifier.multicastEnabled = true;
groupSpecifier.ipMulticastMemberUpdatesEnabled = true;
groupSpecifier.addIPMulticastAddress("225.225.0.1:30303");
// _netGroup = new NetGroup(_netConnection, groupSpecifier.groupspecWithAuthorizations());
// _netGroup.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
_stream = new NetStream(_netConnection, groupSpecifier.groupspecWithAuthorizations());
_stream.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
if(_isListener){
_video.attachNetStream(_stream);
_stream.receiveAudio(true);
// _stream.receiveVideo(false);
_stream.play('sound');
return;
}else{
if(!Microphone.isSupported) return;
_stream.attachAudio(Microphone.getMicrophone());
// var camera:Camera = Camera.getCamera();
// _stream.attachCamera(camera);
_stream.publish("sound", "live");
// _video.attachCamera(camera);
}
}
private function netStatus(event:NetStatusEvent):void{
switch(event.info.code){
case "NetConnection.Connect.Success":
// _isConnected = true;
// _connectedLabel.text = "CONNECTED !";
break;
case "NetGroup.Connect.Success":
_isConnected = true;
_connectedLabel.text = "CONNECTED !";
break;
case "NetStream.Connect.Success":
break;
case "NetStream.Publish.Start":
break;
}
}
private function set listenerStream(value:NetStream):void {
_listenerStream = value;
}
}
}
Thank you in forward.
I was pointed out by Tom Krcha that some networks block multicast. And it was in deed the case.
I got a fresh router to test the apps again and it all worked perfectly.

error in my actionscript for a sound slider I'm working on

Hello I' trying to make a sound slider in flash, while working on the code for it; I keep on getting this error Scene 1, 'S Action', Frame 352, line 8 1152: A conflict exists with inherited definition flash.display:MoveClip.isPlaying in namespace public. I'm wondering what I done to cause this error and how can I fix it?
Please get back to me if you can.
stop();
import flash.media.SoundTransform;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.media.SoundChannel;
var isPlaying:Boolean = true;
var lastPosition:Number = 0;
var mySound:takeachance = new takeachance ;
var myChannel:SoundChannel = new SoundChannel();
var myTransform:SoundTransform = new SoundTransform();
myTransform.volume = .5;
slider02_mc.groove02_mc.scaleX = .5;
myChannel = mySound.play(85,5,myTransform);
myChannel.addEventListener(Event.SOUND_COMPLETE,
soundCompleteHandler);
function soundCompleteHandler(e:Event):void
{
lastPosition=0;
myChannel.stop();
msg_mc.text = "Music stoped playing"
isPlaying = false;
}
play_btn.addEventListener(MouseEvent.CLICK,startSound);
pause_btn.addEventListener(MouseEvent.CLICK,stopSound);
function startSound(myEvent:MouseEvent):void
{
if(!isPlaying)
{
myChannel = mySound.play(lastPosition,5,myTransform);
isPlaying = true;
msg_mc.text="";
myChannel.addEventListener(Event.SOUND_COMPLETE,
soundCompleteHandler);
}
}
function stopSound(myEvent:MouseEvent):void
{
lastPostion = myChannel.position;
myChannel.stop();
isPlaying = false;
}
slider02_mc.mc.addEventListener(MouseEvent.MOUSE_DOWN, myfunction);
function myfunction(event:MouseEvent):void
{
slider02_mc.mc.startDrag(false,new Rectangle(0,0,200,0))
addEventListener(Event.ENTER_FRAME,xpos);
function xpos(event:Event)
{
var myvolume=(slider02_mc.mc.x)/200;
slider02_mc.groove02_mc.scaleX = myvolume;
volume_mc.text = "Volume is"+int(myvolume*100)+"%";
myTransform.volume = myvolume;
myChannel.soundTransform = myTransform;
}
}
stage.addEventListener(MouseEvent.MOUSE_UP,myfunction1);
function myfunction1(event:MouseEvent):void
{
slider02_mc.mc.stopDrag();
addEventListener(Event.ENTER_FRAME,msg);
function msg(event:Event)
{
volume_mc.text="";
}
}
Movieclips already have a property called isPlaying. You should change your variable to something else like soundIsPlaying.

Do you know if there's any problem with netstream.appendBytes() for streaming?

I use netstream.appendBytes to get the streaming (flv) from http, but works intermittently (it works, after refresh, doesn't work, then it works and so on...)
What is the problem?, I have not idea
My code is:
import flash.display.*;
import flash.events.*
import flash.net.*;
import flash.utils.ByteArray;
import com.hurlant.util.Hex;
var videoURL:String = "http://url/vivo/flash";
//elemento de conexíon
var conn:NetConnection = new NetConnection();
conn.connect(null);
//stream de red
var stream:NetStream;
//conexión
stream = new NetStream(conn);
//oyente
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
function Play()
{
var urlStream:URLStream = new URLStream();
//oyentes de URLStream
urlStream.addEventListener(StatusEvent.STATUS, inStatus);
urlStream.addEventListener(Event.COMPLETE, completeHandler);
urlStream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
urlStream.addEventListener(ProgressEvent.PROGRESS, oyenteProcesoCarga);
//urlStream.addEventListener(ProgressEvent.PROGRESS, describeProcesoCarga);
urlStream.addEventListener(IOErrorEvent.IO_ERROR, ioError);
//Video
var video:Video = new Video(640,480);
video.attachNetStream(stream);
addChild(video);
stream.play(null);
urlStream.load(new URLRequest(videoURL));
}
function ioError(event:IOErrorEvent):void
{
textArea.text += event + "\n";
}
function oyenteProcesoCarga(event:ProgressEvent):void
{
var encr:ByteArray = new ByteArray();
event.target.readBytes(encr);
stream.appendBytes(encr);
}
function describeProcesoCarga(event:ProgressEvent):void
{
if (event.target.bytesAvailable > 0){
var encr:ByteArray = new ByteArray();
event.target.readBytes(encr);
}
}
function securityErrorHandler(event:SecurityErrorEvent):void {
}
function asyncErrorHandler(event:AsyncErrorEvent):void {
// ignore AsyncErrorEvent events.
}
function completeHandler(event:Event):void {
}
function inStatus(event:StatusEvent):void {
}
Play();
Maybe use a stream buffer as HTTP is TCP so not all packets arrive at time.