Problem with displaying video when streaming - actionscript-3

I'm having a problem when streaming video. Randomly the video isnt shown, the video is playing though as the playhead moves and audio sounds.
It's strange though because if I press pause then play the video appears and also if I make it fullscreen it appears.
private var videoURL:String = "filename.f4v";
private function setupConnection():void
{
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onErrorConnect);
connection.connect("rtmp://url to my streaming server");
}
private function netStatusHandler(event:NetStatusEvent):void
{
trace("event.info.code "+event.info.code);
switch (event.info.code) {
case "NetConnection.Connect.Success":
connectStream();
break;
case "NetStream.Play.Start":
onPlayVideoHandler();
break;
case "NetStream.Play.StreamNotFound":
trace("Stream not found: " + videoURL);
break;
default :
}
}
private function onErrorConnect(event:AsyncErrorEvent):void
{
trace("onErrorConnect: " + event);
}
private function securityErrorHandler(event:SecurityErrorEvent):void
{
trace("securityErrorHandler: " + event);
}
private function connectStream():void
{
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.bufferTime = 10;
var client:Object = new Object();
client.onMetaData = onMetaData;
stream.client = client;
video = new Video(200, 200);
video.name = "video";
video.addEventListener(Event.ADDED_TO_STAGE, videoAddedToStage)
video.attachNetStream(stream);
video.smoothing = true;
video.x = 0;
video.y = 0;
mainHolder.addChild(video);
stream.play(videoURL, 0, 100, true);
stream.seek(0);
}
private function onPlayVideoHandler():void
{
// add Controls
}
OK Ive found out the reason it doesnt show is because the video sometimes has a width and height of 0 pixels. Anybody know why it would return these values? Is it something todo with the nature of rtmp streaming videos?

I had to listen for the width and height to be greater than zero before proceeding. I never found out why but this is how to fix it.

Related

mute unmute button in actionscript 3

i need some help with my actionscript 3 project. I have a button with a sound in it. I have some code (see below) that when i press the button it plays the sound and if i press the button again it stops the sound (like a mute/unmute button). The problem is that when i press the button to play the sound for the second time it plays two sounds (the same sound twice) and if i press the button to play the sound more times the same sound plays many times. Can you please help me solve the problem? Thank you.
function setMute1(vol){
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
}
var sTransform1:SoundTransform = new SoundTransform(1,0);
var Mute1:Boolean = true;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
function toggleMuteBtn1(event:Event) {
if(Mute1 === false) {
Mute1 = true;
setMute1(0);
} else {
Mute1 = false;
setMute1(1);
}
}
From what I understand you start the sound by assigning it to the buttons hit frame? You need to have the sound started by the code in order to control the sound in good way.
Here is an working example based on your code, that loads an external mp3 file. The sound is played and stopped via the same button.
// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
// Check if sound is muted
if (vol<=0)
{
Mute1 = true;
}
else
{
Mute1 = false;
}
}
// Start/stop sound
function startOrStop()
{
if (Mute1 === false)
{
myChannel.stop();
setMute1(0);
}
else
{
setMute1(1);
myChannel = mySound.play();
}
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
startOrStop()
}
In actionscrip 2 there was a function that would stop all sounds, in actionscript 3 you can't do that anymore, but you can still assign sounds to frames.
This example mutes and unmutes the sound. The sound is't stopped, just muted.
Also here the sound must be assigned in the code, and not to the frame.
// load the sound
var mySound:Sound = new Sound();
mySound.load(new URLRequest("loop.mp3"));
var myChannel:SoundChannel = new SoundChannel();
// tool you need for manipulating the volume;
var sTransform1:SoundTransform = new SoundTransform(1,0);
// The sound starts not muted
var Mute1:Boolean = true;
var vol:Number = 0;
sound1_btn.addEventListener(MouseEvent.CLICK,toggleMuteBtn1);
// Set the sound volume;
function setMute1(vol)
{
sTransform1.volume = vol;
SoundMixer.soundTransform = sTransform1;
// Check if sound is muted
if (vol<=0)
{
Mute1 = true;
}
else
{
Mute1 = false;
}
}
// Toggle mute on/off
function toggleMute()
{
if (Mute1 === false)
{
setMute1(0);
}
else
{
setMute1(1);
}
}
// This happens when you click the buttom
function toggleMuteBtn1(event:Event)
{
// if not playing, the sound
if (myChannel.position != 0) {
} else {
myChannel = mySound.play();
}
toggleMute();
}

Moving object in AS3 while it is translated by Matrix3D

First, please look at my SWF: http://krakow45.pl/spec/warcaby/Warcaby3D.html
You can move pawns and it works pretty well. But the problem starts whem you translate game board (by pressing any of direction keys). After this you cant move pawns. Here is little piece of my code:
translation:
case Keyboard.LEFT:
_matrix = new Matrix3D();
_matrix.appendTranslation(0, -200, 0);
_matrix.appendRotation(_rot++, Vector3D.X_AXIS);
_matrix.appendTranslation(0, 200, _depth);
_table._board.transform.matrix3D = _matrix;
break;
moving the pawn:
private function MouseDown(event:MouseEvent):void
{
var pawn:Pawn = event.currentTarget as Pawn;
_xPos = pawn._xPos;
_yPos = pawn._yPos;
_txt.text = pawn._xPos + " - " + pawn._yPos + "\n";
pawn.startDrag();
}
Ok, I solved this by using something like this: (against startStag() )
private var _clicked:Boolean
private var _currentPawn:Pawn
private function MouseDown(event:MouseEvent):void
{
_clicked = true;
_currentPawn = event.currentTarget as Pawn;
// rest of my code
}
private function MouseMove(event:MouseEvent):void
{
if(_clicked)
{
_currentPawn.x = mouseX;
_currentPawn.y = mouseY;
}
}
private function MouseUp(event:MouseEvent):void
{
_clicked = false;
// rest of my code
}

ActionScript 3: LoaderInfo COMPLETE event doesn't fire after load() and loadBytes()

I'm trying to load PNG images with ActionScript with a Loader object. This works fine for some of the images (the INIT and COMPLETE events are fired as expected), for some other it doesn't. I've read in this thread that a URLLoader might help, so I tried that, using the loadBytes() function afterwards. Still doesn't work: the URLLoader fires the COMPLETE event, but the LoaderInfo object does not.
I've written a sample class that demonstrates the problem with two files (one working, the other one not).
public class LoaderTest extends MovieClip {
var output:TextField;
var loader:Loader;
var urlLoader:URLLoader;
function LoaderTest() {
output = new TextField();
output.width = 1000;
output.height = 1000;
output.multiline = true;
addChild(output);
var t1:Timer = new Timer(0, 0);
t1.addEventListener(TimerEvent.TIMER, function() {
t1.stop(); loadMapDirect("map_in_big.png");
});
var t2:Timer = new Timer(1000, 0);
t2.addEventListener(TimerEvent.TIMER, function() {
t2.stop(); loadMapDirect("map_us_big.png");
});
var t3:Timer = new Timer(2000, 0);
t3.addEventListener(TimerEvent.TIMER, function() {
t3.stop(); loadMapBytes("map_in_big.png");
});
var t4:Timer = new Timer(3000, 0);
t4.addEventListener(TimerEvent.TIMER, function() {
t4.stop(); loadMapBytes("map_us_big.png");
});
t1.start();
t2.start();
t3.start();
t4.start();
}
function loadMapBytes(url:String):void {
try {
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(ProgressEvent.PROGRESS, progressListener);
urlLoader.addEventListener(Event.COMPLETE, completeListenerBytes);
output.appendText("\nLoading '"+url+"' with URLLoader ");
urlLoader.load(new URLRequest(url));
} catch (error:Error) {
output.appendText("Err: " + error.message + "\n");
}
}
function completeListenerBytes(e:Event):void {
output.appendText("COMPLETE Event fired for URLLoader!\n");
try {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListenerDirect);
output.appendText("Loading bytes with Loader ");
loader.loadBytes(e.target.data as ByteArray);
} catch (error:Error) {
output.appendText("Err: " + error.message + "\n");
}
}
function loadMapDirect(url:String):void {
try {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListenerDirect);
output.appendText("\nLoading '"+url+"' with Loader ");
loader.load(new URLRequest(url));
} catch (error:Error) {
output.appendText("Err: " + error.message + "\n");
}
}
function completeListenerDirect(e:Event):void {
var bmd:BitmapData = Bitmap(e.target.loader.content).bitmapData;
output.appendText("COMPLETE Event fired for Loader! => h: " + bmd.height + ", w: " + bmd.width + "\n");
}
function progressListener (e:ProgressEvent):void{
output.appendText(".");
if (e.bytesLoaded == e.bytesTotal) {
output.appendText(" progress complete, " + e.bytesTotal + " bytes loaded!\n");
}
}
}
All images were generated with the PHP GD library and I'm compiling with SWFTools's as3compile.
You can see the script it in action on http://www.wichte-sind-wichtig.de/as3loader/loaderTest.swf
The two images map_in_big.png and map_us_big.png are in the same folder (not allowed to post more hyperlinks).
Any ideas?
The problem is that your application is probably compiled for Flash Player 9. In version 9 the maximum allowed image dimensions are 2880 x 2800 and map_us_big.png is 3150 x 1570. I ran the application successfully when I compiled it for Flash Player 10.
Here's a reference http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#BitmapData%28%29
In AIR 1.5 and Flash Player 10, the maximum size for a BitmapData
object is 8,191 pixels in width or height, and the total number of
pixels cannot exceed 16,777,215 pixels. (So, if a BitmapData object is
8,191 pixels wide, it can only be 2,048 pixels high.) In Flash Player
9 and earlier and AIR 1.1 and earlier, the limitation is 2,880 pixels
in height and 2,880 pixels in width. If you specify a width or height
value that is greater than 2880, a new instance is not created.

Recording sound in FMS

hello
I want to save a loaded sound in FMS .
public function Record()
{
nc.connect("rtmp://192.168.1.2:1935/videoRecorder");
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
play_btn.visible = stop_btn.visible = start_btn.visible = false;
}
function netStatusHandler(event:NetStatusEvent):void
{
t1_txt.text = event.info.code;
trace(event.info.code);
switch (event.info.code)
{
case "NetConnection.Connect.Success" :
connectStream();
break;
case "NetStream.Play.StreamNotFound" :
//trace("Stream not found: " + videoURL);
break;
}
}
function connectStream()
{
ns = new NetStream(nc);
var mic:Microphone = Microphone.getMicrophone();
var cam:Camera = Camera.getCamera();
if (cam)
{
cam.setMode(400,300,15,false);
cam.setQuality(0,100);
ns.attachAudio(mic);
ns.attachCamera(cam);
video.attachCamera(cam);
video.height = 300;
video.width = 400;
addChild(video);
start_btn.visible = true;
start_btn.addEventListener(MouseEvent.MOUSE_UP,startRecord);
}
else
{
t1_txt.text = "No camera attached";
}
}
This is my code. But i need to save my loaded sound insted of mic . Is it possible?
There is no way to attach a Sound to a NetStream.
You can solve your issue in the following way:
remove the attachAudio part from your code
when the video recording is completed, use a web or FTP server to upload the sound file to FMS (there are no other ways to upload files to FMS).
play back the audio and the video at the same time (or you can mix them with FFMPEG)
Cheers
Tamas Gronas

Flash As3 Loader Problem

Hi iam trying to load a few external jpegs in As3.
It works fine locally in Flash, but it dosen't work at all ion my server.
My app also loads a youtube video simultaneously.
function drawResult(index,videoID,song_title,thumbnail:String=null)
{
var theClip:resultRowClip=new resultRowClip ();
_clip.addChild(theClip);
myArray[index] = new Array(videoID,theClip);
theClip.y=0+(43*(index-1));
theClip.rowText.text = song_title;
theClip.rowBack.visible = false;
if (thumbnail != ""){
theClip.tHolder.visible=true;
loadImage(thumbnail,index);
}
}
function loadImage(url:String,index):void
{
//this.myClip.myText.text += "load image";
// Set properties on my Loader object
var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest(url));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function (evt:Event){imageLoaded(evt,index)});
}
function imageLoaded(evt,id):void
{
//this.myClip.myText.text += "id : evt : " + evt.status;
// Load Image
var image:Bitmap = new Bitmap(evt.target.content.bitmapData);
myArray[id][1].tHolder.addChild(image);
myArray[id][1].tHolder.width=myArray[id][1].tHolder.width*0.35;
myArray[id][1].tHolder.height=myArray[id][1].tHolder.height*0.35;
}
Does anyone knows what the problem is ?
** I added two Evenet listeners from io Error :
imageLoader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
This is the function for handling the errors :
private function ioErrorHandler(event:IOErrorEvent):void {
this.myClip.myText.text +=("ioErrorHandler: " + event);
}
anyway, I got no errors...
I also tried to move the listeners before imageLoader.load but it's still the same...no errors and no data loaded..
I change my code to patrikS suggestion :
function loadImage(url:String,index):void
{
//this.myClip.myText.text += "load image";
// Set properties on my Loader object
//if (index != 1) return;
var imageLoader:Loader = new Loader();
imageLoader.name = index.toString();
//myArray[index][1].addChild(imageLoader);
//imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function (evt:Event){imageLoaded(evt,index)});
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
imageLoader.load(new URLRequest(url));
}
My current completeHandler function(tnx patrikS ) :
private function completeHandler(evt:Event):void{
//this.myClip.myText.text += "id : evt : " + evt.status;
// Load Image
trace("evt target loader name : "+ evt.target.loader.name );
evt.target.removeEventListener(Event.COMPLETE, completeHandler );
var image:Bitmap = new Bitmap(evt.target.content.bitmapData);
myArray[evt.target.loader.name][1].tHolder.addChild(image);
myArray[evt.target.loader.name][1].tHolder.width=myArray[evt.target.loader.name][1].tHolder.width*0.35;
myArray[evt.target.loader.name][1].tHolder.height=myArray[evt.target.loader.name][1].tHolder.height*0.35;
//trace (hadar.y + "Y / X" + hadar.x);
}
It still work only on flash IDE and dosent work on any browser...
try urlstream and check crossdomain.xml :)
urlstream = new URLStream();
urlstream.addEventListener(Event.COMPLETE, onLoad);
urlstream.addEventListener(IOErrorEvent.IO_ERROR, onErr);
urlstream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onErr);
urlstream.load(req);
private function onLoad(e:Event):void {
var ba:ByteArray = new ByteArray();
urlstream.readBytes(ba, 0, urlstream.bytesAvailable);
_loader.contentLoaderInfo.addEventListener(Event.INIT, onBytesLoad);
_loader.loadBytes(ba);
}
Listeners should be added before calling the load() method.
Also there's no real advantage in using a closure for the complete event listener & think of removing the event listeners!
function loadImage(url:String, index:int):void
{
//this.myClip.myText.text += "load image";
// Set properties on my Loader object
var imageLoader:Loader = new Loader();
imageLoader.name = index.toString();
//make sure you to add your listeners here!
imageLoader.contentLoaderInfo.addEventListener(
IOErrorEvent.IO_ERROR,ioErrorHandler);
imageLoader.contentLoaderInfo.addEventListener(
Event.COMPLETE, completeHandler );
imageLoader.load(new URLRequest(url));
}
function completeHandler(event:Event ):void
{
//imageLoaded(evt,index);
trace( event.target.loader.name );
event.target.removeEventListener(Event.COMPLETE, completeHandler );
}
In debug model, you can load files from any avaliable site, but release model.
may be this help : http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html