Audio stream reconnect in as3 - actionscript-3

I am using the following code in a swf created with Flash CS6:
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
var customClient:Object = new Object();
ns.client = customClient;
ns.play("http://streamserver3.us:7018/stream?type=.flv");
It plays an Icecast audio streaming.
How could I do to reconnect automatically (if the internet connection drops, etc.)?
Thank you very much for your help!
Sincerely.

See if any of these NetStreamInfo options can help you detect a connection drop:
Try using a timer that fires every two seconds where the responding function checks if there has been a change (ie: amount of incoming data has become zero).
Maybe check with ns.info.dataBytesPerSecond but explore the descriptions of other methods in that linked Info Class. Use an If statement to check input has not become zero or Else (if now zero) run your function that reconnects (does usual ns.stop(); & ns.play( url ); etc..).

Thank you very much for your help. I have found a solution that works and I would like to share:
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
var customClient:Object = new Object();
ns.client = customClient;
ns.play("http://xxxxxxxxx/stream?type=.flv");
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
function netStatusHandler(event:NetStatusEvent):void
{
switch (event.info.code)
{
case 'NetStream.Play.Stop' :
MovieClip(this.root).gotoAndPlay(1, "Scene 2");
break;
case "NetStream.Play.StreamNotFound" :
MovieClip(this.root).gotoAndPlay(1, "Scene 2");
break;
}
}
I have created a new scene "Scene 2" with the following code:
MovieClip(this.root).gotoAndPlay(1, "Scene 1");
Now if the stream (internet or server connection) is lost, the player will reconnect automatically to the stream.
Working great!
Thank you very much for your ideas!
Regards

Related

play a external flv video

Hello please someone can help me with this ...
I want to play a external flv video ("../sync/video/video.flv"), but in case the video is missing or when there is a (StreamNotFound) error
I want to play automatically another flv video.
case "NetStream.Play.StreamNotFound":
ns.play("../sync/filler/video2.flv");
but it doesn't work ....
here is the full code :
var vid:Video;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
var customClient:Object = new Object();
customClient.onMetaData = metaDataHandler;
ns.client = customClient;
ns.play("../sync/video/video.flv");
vid = new Video();
vid.attachNetStream(ns);
addChild(vid);
function netStatusF(e:NetStatusEvent):void
{
switch (e.info.code)
{
case "NetStream.Play.StreamNotFound" :
ns.play("../sync/filler/video2.flv");
break;
}
}
function metaDataHandler(infoObject:Object):void
{
vid.width = infoObject.width;
vid.height = infoObject.height;
}
You have just to add a NetStatusEvent.NET_STATUS event listener to your NetStream object :
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusF);
Then you have to assure that your second video file exist, otherwise you'll have a looping problem.
Hope that can help.

AS3 play multy video files

I have this code for playing flv video when swf starts. How can I make it play 2 or 3 flv videos in sequence? here is the code which loads flv and plays it so I need to play two more videos after the first one automatically.
var vid:Video = new Video(1080, 720);
addChild(vid);
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
vid.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData = function(evt:Object):void {};
ns.client = listener;
ns.play("Postvideo1.flv");
You are very close. NetStream client will help you to solve this task. More details on onPlayStatus
Establishes a listener to respond when a NetStream object has completely played a stream.
var listener:Object = {};
listener.onMetaData = function (meta:Object):void {
//Video duration
trace(meta.duration);
};
listener.onPlayStatus = function (data:Object):void {
if (data.code == "NetStream.Play.Complete") {
trace("Video playback is completed!");
//Good place to initiate playback of another video
}
}
ns.client = listener;

Streaming audio and video AS3

I have couple of videos that I'm streaming. I start with the video_1.flv and after it finishes the video_2.flv is running and looping until the user takes some action to play other movie for example video_3.flv. So I need when video_2.flv is playing in the background to be played audio file sound.mp3 witch is going to be looped , witch i managed to do. But I need both of them to run independently. Because right now when the video is looped the audio is looping to. And I need the audio to be playd only when video_2.flv is played. Thank YOU.
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
loader.vid.Video_1.attachNetStream(ns);
var listener:Object = new Object();
listener.onMetaData = function(evt:Object):void {};
ns.client = listener;
ns.play("http://media.easyads.bg/ads/display_ads_richmedia/video/avon/maria_ilieva/video_1.flv");
ns.addEventListener(NetStatusEvent.NET_STATUS, NCListener);
var clipTimer:Timer = new Timer(4000);
function NCListener(e:NetStatusEvent){
if (e.info.code == "NetStream.Play.Stop") {
ns.play("http://media.easyads.bg/ads/display_ads_richmedia/video/avon/maria_ilieva/video_2.flv");
sound.load(req);
shaker(null);
}
};
var sound:Sound = new Sound();
var soundChannel:SoundChannel;
var req:URLRequest = new URLRequest("sound.mp3");
sound.addEventListener(Event.COMPLETE, onSoundLoadComplete);
function onSoundLoadComplete(e:Event):void{
sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete);
soundChannel = sound.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
}
function onSoundChannelSoundComplete(e:Event):void{
e.currentTarget.removeEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
soundChannel = sound.play();
}
loader.button_01.addEventListener(MouseEvent.CLICK, play_video_01);
loader.button_01.addEventListener(MouseEvent.ROLL_OVER, play_effect_01);
function play_video_01 (event:MouseEvent){
clipTimer.stop();
ns.play("http://media.easyads.bg/ads/display_ads_richmedia/video/avon/maria_ilieva/video_3.flv");
loader.button_01_mc.gotoAndPlay (41);
}
Maybe this tutorial is helpful to you...
I'm new in AS but I'd rather prefer to pack the whole s.... stuff into a (reusable) class...
Behavior of a Loaded SWF File versus Behavior of a MovieClip
Greets
Gee

How i know if a Sound object is playing?

In AS3 i've created a code that load a sound and execute it in streaming.
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
mySound.load(new URLRequest("surrender.mp3"));
myChannel = mySound.play();
Considering that the mp3 file is placed on the server, i need to trace if it's already begun or still receiving initial data from the server. Once it's effectively started i've to call a function that initialize some things.
the problem is, how i can trace if a sound or channel object "is playing" ?
You can check this by listening for the different Events the Sound object dispatches as well as its properties like Sound.isBuffering
Refer to the Sound manpage
you could wait for the mp3 to be loaded completely
var myChannel:SoundChannel;
var soundFactory:Sound = new Sound();
soundFactory.addEventListener(Event.COMPLETE, completeHandler);
soundFactory.load(request);
// ...
private function completeHandler(event:Event):void {
trace("completeHandler: " + event);
myChannel = soundFactory.play();
}
or you can use a Timer and check the myChannel.position periodically - if it doesn't change the sound stopped playing...

Weird NetStream problem when using function

This code causes my f4v file to stop playing prematurely. Time changes but roughly 8-10 seconds in.
loadSong();
function loadSong()
{
if(!songPlaying)
{
songPlaying = true;
var customClient:Object = new Object();
customClient.onCuePoint = cuePointHandler;
customClient.onMetaData = metaDataHandler;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = customClient;
ns.play("song.f4v");
}
trace("HERE");
}
function cuePointHandler(infoObject:Object):void{
trace(infoObject.name);
}
function metaDataHandler(infoObject:Object):void {
trace("metaData");
}
This code let's the f4v play till the end. WTF!? It seems that when I call it via a function it causes the issue. FYI the code is stored in the first frame of the main timeline, and the F4v is audio only. Any help would be appreciated.
if(!songPlaying)
{
songPlaying = true;
var customClient:Object = new Object();
customClient.onCuePoint = cuePointHandler;
customClient.onMetaData = metaDataHandler;
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = customClient;
ns.play("song.f4v");
}
What is happening, when you declare your NetConection and NetStream inside the function, is that the scope for that variable is local to that function. That means that nothing else is referencing the NetConnection you created and thus the garbage collector sweeps it up during its next run (that's why you are seeing the variable time).
When you declare it in just an if statement the variables are in the scope of the movie and that holds a reference to them and thus aren't garbage collected.
I don't know what the architecture is for the rest of your code, but if you want to use functions to hold your code, try putting a declaration for the var nc:NetConnection = new NetConnection(); just before the loadSong(); statement.
Architecturally, you might want to refactor your code out of the frame, but it might really not be worth it, if it is just couple of lines of code. Just depends on your project.
For more information about garbage collection, check out Understanding garbage collection in Flash Player 9 (It's says Flash Player 9, but this is applicable to 10 as well).