How to pause and restart a NetStream object? - actionscript-3

I have a NetStream object, based upon the following code:
streamID = "mystreamkey";
videoURL = "rtmp://mystreamurl";
vid = new Video();
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
nc.client = { onBWDone: function():void{}, streamConnected: streamConnected, streamDisconnected: streamDisconnected };
nc.connect(videoURL);
...
metaListener = new Object();
metaListener.onMetaData = received_Meta;
netStreamObj.client = metaListener;
netStreamObj.play(streamID);
vid.attachNetStream(netStreamObj);
addChild(vid);
All this is working fine, and I can also use netStreamObj.pause() to pause the live stream.
But how do I restart it? netStreamObj.play() won't work - it requires one parameter, so then I wrote this:
netStreamObj.play(streamID);
vid.attachNetStream(netStreamObj);
But this isn't working either.
How do I restart the stream, so that it connects to the live stream it was previously connected to?

How do I restart the stream, so that it connects to the live stream it
was previously connected to?
For future reference, check the NetStream API documentation for options. You'll be looking to check out the commands listed under the Public methods section.
You can use either :
resume(): Resumes playback of a video stream that is paused.
usage : netStreamObj.resume();
togglePause(): Pauses or resumes playback of a stream.
usage : netStreamObj.togglePause();
I suggest using togglePausesince it auto-detects the playback state of the NetStream object. Your "Pause" button (or MovieClip) should simply have a click listener event whose function has this line : netStreamObj.togglePause();. It's that easy...

Related

Streaming FLV videos

I have 3 flv videos that I'm streaming. The first one is the intro. Then the second one is playing after the intro right after the intro ends. And the second video is looping. Everything seemed to be fine. But some times when i load the swf it starts from the second video. Any ideas why ?
import flash.events.MouseEvent
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("video_1.flv");
ns.addEventListener(NetStatusEvent.NET_STATUS, NCListener);
function NCListener(e:NetStatusEvent){
if (e.info.code == "NetStream.Buffer.Empty") {
ns.play("video_2.flv");
}
};
loader.button_01.addEventListener(MouseEvent.CLICK, play_video_01);
loader.button_01.addEventListener(MouseEvent.ROLL_OVER, play_effect_01);
function play_video_01 (event:Event):void{
ns.play("video_3.flv");
loader.button_01_mc.gotoAndPlay (21);
}
function play_effect_01 (event:Event):void{
loader.button_01_mc.gotoAndPlay (2);
}
In your code, playback of your second video is triggered by the message NetStream.Buffer.Empty. That message can get dispatched for several reasons, as well as when playback of a video ends. For example when streaming (which I know you're not doing), NetStream.Buffer.Empty can get dispatched when there is a network problem. This is definitely the cause of your problem, but it's not clear why sometimes the buffer empty message gets dispatched right away.
The first thing I would do is modify your NetStatusEvent listener so that it traces out all of the messages that are being dispatched. That way you can see the sequence of events that occurs when this problem happens.
And second, you should try using another message to trigger playback of the second video. I'm not 100% sure, but I think the message NetStream.Play.Stop is what you want (this gets dispatched when the end of the video is reached, as well as when you programmatically stop playback). The full list of messages you get from a NetStatusEvent is here.
Incorporating both of these suggestions, your NetStatusEvent handler might look like this:
function NCListener(e:NetStatusEvent)
{
var code:String = e.info.code;
trace("code: ", code);
if (code == "NetStream.Record.Stop"
ns.play("video_2.flv");
}
Finally, you might want to add other event listeners to the NetStream. It dispatches an IOErrorEvent and AsyncErrorEvent ... perhaps you're getting one of these when the problem happens.

How to cache audio stream data and access it in AS3

I'm streaming an MP3 file in AS3. All is working fine (I can play it) but I'm looking to implement a 'seek' bar. This means I will need to cache the file (as it's being downloaded) and then access the cached data when the user seeks a specific time in the song.
The code to actually play the mp3 stream:
function openStream( stream )
{
var s:Sound = new Sound();
var req:URLRequest = new URLRequest(stream);
var context:SoundLoaderContext = new SoundLoaderContext(500, true);
s.load(req, context);
s.play();
}
So how would I cache the file as it's being downloaded and then access the data from the cache?
I know this is pretty far from a trivial task, so I would be grateful if you could even just provide a few links to some tutorials/docs/articles.
You do not need to cache the sound for this.
The downloaded sound data is is available as long as the sound object lives in memory.
So all you need to do is take the sound object outside the function into the class scope..
Also the play function returns the current SoundChannel used by the Sound.
private var snd:Sound = new Sound();
private var channel:SoundChannel;
function openStream( stream ) {
...
channel = snd.play();
}
To implement the seek functionality you may make use of,
bytesLoaded (To know how much of the sound is downloaded)
soundChannel.position (To know current sound position)

flash/AS3 netstream loading/buffering very slow, what am I missing?

My custom movieplayer uses the following code for playing video. It takes a really long time for the clip to start playing, but once it does, you can skip directly to the end.
I have a feeling that there is some knowledge I'm missing in how buffers and preload works. Could somebody send me in the right direcion?
private function init(e:Event = null):void {
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS, doNetStatus);
connection.addEventListener(IOErrorEvent.IO_ERROR, doIOError);
connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doSecurityError);
connection.connect(null);
stream = new NetStream(connection);
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, doAsyncError);
stream.addEventListener(NetStatusEvent.NET_STATUS, doNetStatus);
stream.addEventListener(IOErrorEvent.IO_ERROR, doIOError);
stream.client = this;
video = new Video(1024, 576);
mc = new MovieClip();
mc.addChild(video);
stage.addChild(mc);
mc.addEventListener(MouseEvent.CLICK, onClick);
video.attachNetStream(stream);
stream.bufferTime = 5;
stream.receiveAudio(true);
stream.receiveVideo(true);
stream.play(SITEURL + vidID +".mp4");
}
You can see the player in action here: http://joon.be/serve/ngjd_player.swf
Apparently the video's weren't streaming because I needed to run QT FastStart on them.
This solved the problem, netstream is now running as expected.
You can download qt-faststart.exe and then in admin command promt run:
qt-faststart.exe "source.mp4" "fixed.mp4"
Took about 15-30 seconds for 10 minutes long video.
Credit: https://articulate.com/support/article/mp4-movie-doesnt-begin-playing-until-it-has-fully-downloaded
I also tried this free opensource converter HandBrake with Web Optimized checked. (took all 10 minutes to convert)

Destroy camera in as3

I am using the Camera on Flash, but when I finish to use, now can I "destroy" the camera object for improve the application performance?
private var camera_atual:Camera = Camera.getCamera();
private var video_camera:Video = new Video(820, 546);
camera_atual.setQuality(0,100);
camera_atual.setMode(550,480,30,true);
video_camera.attachCamera(camera_atual);
this.palco_mc.addChild(video_camera);
See Video.attachCamera() - "To drop the connection to the Video object, pass null."
The camera itself should be destroyed manually :)

if I load a flv with netStream, how can I call a function when the flv stops playing

I have a website in ActionScript 3 that has lots of FLV animations that happen when you press buttons. Right now this is how I have it set up.
in AS3,
im loading FLv's (which are animations I exported in FLV form from After Effects)
with net stream. I have a timer set up for the same amount of length of time that the animations (FLV's) play and when the timer stops it calls a function that closes the stream, opens a new one and plays another video. The only problem I noticed using timers is that if the connection is slow and (animation)stops for a second, the timer keeps going, and calls the next flv too early.
Does anyone know a way to load a flv, or swf for that matter, at the end of play of the flv? so that the next FLV will always play at the end of the run time of the previous FLV, rather than using timers?
im thinking onComplete but I don't know how to implement that!?
Sequential playing is pretty easy to achieve with the OSMF framework, you should check it out. Google "osmf tutorials" and you should find a few tutorials online.
The framework is fairly recent, but it looks like it may become the de facto solution for media delivery in Flash as it's not limited to video but also audio & images.
As a developer you won't have to bother with the NetStream & NetConnection classes. Developing video solutions , as well as audio & images solutions should be streamlined and easier to handle. Only limitation is that it requires Flash 10
Here's some code for checking when a FLV ends with NetStream. I just provide snippets as I assume you got the FLV up and running already.
//create a netstream and pass in your connection
var netStream:NetStream = new NetStream(conn);
//add callback function for PlayStatus -event
var client : Object = {};
client.onPlayStatus = onPlayStatus;
netStream.client = client;
//attach your NetStream to the connection as usual
//---
//function that gets called onPlayStatus
function onPlayStatus(info : Object) : void {
trace("onPlayStatus:" +info.code + " " + info.duration);
if (info.code == "NetStream.Play.Complete") {
//play the next FLV and so on
}
}
EDIT: With your example code it will look something like this.
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
var listener:Object = new Object();
listener.onMetaData = function(md:Object):void{};
listener.onPlayStatus = function(info : Object) : void {
trace("onPlayStatus:" +info.code + " " + info.duration);
if (info.code == "NetStream.Play.Complete") {
//play the next FLV and so on
}
};
ns.client = listener;
vid1.attachNetStream(ns);
const moviename1:String = "moviename2.flv";
const moviename1:String = "moviename3.flv";
var movietoplay:String = "moviename.flv";
ns.play(movietoplay);