A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: INTERESTING: Flash Bug? setInterval stops flv playback!?

  1. #1
    Member
    Join Date
    Aug 2008
    Posts
    85

    Cow Icon INTERESTING: Flash Bug? setInterval stops flv playback!?

    I have a function that is called every 3 seconds using setInterval. I've spent about an hour figuring out why my flv video is pausing itself at random times. I did this by removing code and ruling things out. It seems that any usage of setInterval while playing an .flv file will cause the video randomly pause itself. Sometimes it's after 3 seconds, sometimes 12, sometimes 30.. completely random.

    It seems I am not the only one with this problem. Unfortunately though, neither of these threads (only two I could find online) have a solution.

    http://www.actionscripts.org/forums/....php3?t=169999
    http://www.mail-archive.com/flashcod.../msg05502.html

    Why would setInterval randomly pause flv playback? I'm rather suprised that there is not more info online about this bug, as I'm sure a large number of people are using setInterval and also playing an flv file.

    Try this code for yourself and you should see what I mean. You will need to save this to a folder and copy a .flv file to the same folder for this to work (as with any script that includes files). It might not work on the first or second time, but do the "Test Movie" in Flash a few times and you should see this happen.

    Code:
    function getMessages() {
    	
    	trace("got messages");
    
    }
    
    getMessages();
    setInterval(getMessages, 3000);// 3 sec
    Code:
    function playCommercial():void {
    
    	trace("Commercial is now playing");
    
    	var video2:Video=new Video(320,240);
    	addChild(video2);
    	video2.x=10;
    	video2.y=10;
    
    	var nc2:NetConnection = new NetConnection();
    	nc2.connect(null);
    
    	var ns2:NetStream=new NetStream(nc2);
    	ns2.addEventListener(NetStatusEvent.NET_STATUS, onStatusEvent);
    
    	function onStatusEvent(stat:Object):void {
    		trace(stat.info.code);
    		if (stat.info.code=="NetStream.Play.Stop") {
    			trace("Commercial has ended");
    			video2.visible=false;
    		}
    	}
    
    	var meta:Object = new Object();
    	meta.onMetaData = function(meta2:Object) {
    		trace(meta.duration);
    	};
    
    	ns2.client=meta;
    
    	video2.attachNetStream(ns2);
    
    	ns2.play("YOUR_VIDEO_FILE_HERE.flv");
    
    }
    
    playCommercial();
    Last edited by sufire; 08-01-2009 at 02:34 AM.

  2. #2
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    Didn't have the problem here at my end and I tested the movie quite a few times!

    Maby you could use a timer instead?
    PHP Code:
    var t:Timer = new Timer(3000);
    t.addEventListener(TimerEvent.TIMERgetMessages);

    function 
    getMessages(e:TimerEvent):void
    {
       
    trace("got message " t.currentCount);

    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

  3. #3
    Member
    Join Date
    Aug 2008
    Posts
    85
    I just tried that and no luck. But it seems I stand corrected. I now think the issue is that I have two NetConnection's and two NetStream's. One connects to a FMS server and plays a live stream. Then after a short amount of time, a setInterval or Timer (I've tried both) tells the live stream to pause, add the commercial player (addChild) to the stage, connect to NULL and play a commercial.flv file. When the FLV is done, the commercial player disappears (visible = false) and the live stream is resumed (ns.resume).

    I now think that maybe because I have two NetStreams/NetConnections that this may be causing the flv video to pause at a random time. I have named my vars ns and ns2 and nc and nc2, so I don't understand why there is any interference.

    I tried simplifying my code but it's hard. I'm afraid this may be too much to read. Ignore all the mute/volume/stop/play stuff.. thats most of the code.

    Code:
    stop();
    
    var nc:NetConnection;
    var ns:NetStream;
    var liveVideo:Video;
    
    //start volume when initializing player
    var DEFAULT_VOLUME:Number=1.0;
    
    //update delay in milliseconds
    var DISPLAY_TIMER_UPDATE_DELAY:int=10;
    
    //timer for updating player volume
    var tmrDisplay:Timer;
    
    //flag for volume scrubbing
    var bolVolumeScrub:Boolean=false;
    
    //holds the last used volume, but never 0
    var intLastVolume:Number=DEFAULT_VOLUME;
    
    //object holds all meta data
    var objInfo:Object;
    
    
    liveVideoPlayer.streamTitle.text="Connecting..";
    
    
    nc = new NetConnection();
    
    nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
    
    function onConnectionStatus(e:NetStatusEvent):void {
    	if (e.info.code=="NetConnection.Connect.Success") {
    		liveVideoPlayer.streamTitle.text="Buffering..";
    		getTheStream();
    	}
    }
    
    function getTheStream():void {
    	ns=new NetStream(nc);
    	ns.client=this;
    	liveVideoPlayer.liveVideo.attachNetStream(ns);
    	ns.bufferTime=1;
    	ns.play("livestream");
    }
    
    function onMetaData(info:Object):void {
    	if (! tmrDisplay.running) {
    		tmrDisplay.start();
    	}
    
    	objInfo=info;
    
    	liveVideoPlayer.streamTitle.text=info.title;
    }
    
    NetConnection.prototype.onBWDone = function(p_bw) {
    //trace("onBWDone: "+p_bw);
    };
    
    nc.connect("rtmp://12.34.56.789/live");
    
    liveVideoPlayer.btnPlay.visible=false;
    liveVideoPlayer.btnUnmute.visible=false;
    
    liveVideoPlayer.btnPlay.addEventListener(MouseEvent.CLICK, playClicked);
    liveVideoPlayer.btnStop.addEventListener(MouseEvent.CLICK, stopClicked);
    
    
    
    
    // ----- FUNCTIONS
    
    function playClicked(e:MouseEvent):void {
    	ns.resume();
    	liveVideoPlayer.btnStop.visible=true;
    	liveVideoPlayer.btnPlay.visible=false;
    }
    
    function stopClicked(e:MouseEvent):void {
    	ns.pause();
    	liveVideoPlayer.btnStop.visible=false;
    	liveVideoPlayer.btnPlay.visible=true;
    	liveVideoPlayer.streamTitle.text="Stopped";
    }
    
    function initVideoPlayer():void {
    
    	stage.addEventListener( MouseEvent.MOUSE_UP, mouseReleased);
    
    	liveVideoPlayer.btnMute.addEventListener(MouseEvent.CLICK, muteClicked);
    	liveVideoPlayer.btnUnmute.addEventListener(MouseEvent.CLICK, unmuteClicked);
    	liveVideoPlayer.mcVolumeScrubber.btnVolumeScrubber.addEventListener(MouseEvent.MOUSE_DOWN, volumeScrubberClicked);
    
    	liveVideoPlayer.mcVolumeScrubber.x = (52 * DEFAULT_VOLUME) + 401;
    	liveVideoPlayer.mcVolumeFill.mcFillRed.width=liveVideoPlayer.mcVolumeScrubber.x-452+52;
    
    	tmrDisplay=new Timer(DISPLAY_TIMER_UPDATE_DELAY);
    	tmrDisplay.addEventListener(TimerEvent.TIMER, updateDisplay);
    }
    
    function muteClicked(e:MouseEvent):void {
    	setVolume(0);
    
    	liveVideoPlayer.mcVolumeScrubber.x=401;
    	liveVideoPlayer.mcVolumeFill.mcFillRed.width=1;
    }
    
    function unmuteClicked(e:MouseEvent):void {
    	setVolume(intLastVolume);
    
    	liveVideoPlayer.mcVolumeScrubber.x = (52 * intLastVolume) + 401;
    	liveVideoPlayer.mcVolumeFill.mcFillRed.width=liveVideoPlayer.mcVolumeScrubber.x-452+52;
    }
    
    function volumeScrubberClicked(e:MouseEvent):void {
    	bolVolumeScrub=true;
    
    	liveVideoPlayer.mcVolumeScrubber.startDrag(false, new Rectangle(401, 288, 52, 0));
    }
    
    function mouseReleased(e:MouseEvent):void {
    	bolVolumeScrub=false;
    
    	liveVideoPlayer.mcVolumeScrubber.stopDrag();
    
    	liveVideoPlayer.mcVolumeFill.mcFillRed.width=liveVideoPlayer.mcVolumeScrubber.x-452+52;
    
    	if ((liveVideoPlayer.mcVolumeScrubber.x - 401) / 52 > 0) {
    		intLastVolume = (liveVideoPlayer.mcVolumeScrubber.x - 401) / 52;
    	}
    }
    
    function setVolume(intVolume:Number = 0):void {
    
    	var sndTransform=new SoundTransform(intVolume);
    
    	ns.soundTransform=sndTransform;
    
    	if (intVolume>0) {
    		liveVideoPlayer.btnMute.visible=true;
    		liveVideoPlayer.btnUnmute.visible=false;
    	} else {
    		liveVideoPlayer.btnMute.visible=false;
    		liveVideoPlayer.btnUnmute.visible=true;
    	}
    }
    
    function updateDisplay(e:TimerEvent):void {
    	if (bolVolumeScrub) {
    		setVolume((liveVideoPlayer.mcVolumeScrubber.x - 401) / 52);
    		liveVideoPlayer.mcVolumeFill.mcFillRed.width=liveVideoPlayer.mcVolumeScrubber.x-452+52;
    	}
    
    	liveVideoPlayer.timeText.htmlText=formatTime(ns.time);
    }
    
    function formatTime(t:int):String {
    	var s:int=Math.round(t);
    	var m:int=0;
    	if (s>0) {
    		while (s > 59) {
    			m++;
    			s-=60;
    		}
    		return String((m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s);
    	} else {
    		return "00:00";
    	}
    }
    
    initVideoPlayer();
    
    // ------------------------------------
    // ----- TABS -------------------------
    // ------------------------------------
    
    // removed, irrelivant
    
    // ------------------------------------
    // ----- CHAT ROOM --------------------
    // ------------------------------------
    
    // removed, irrelivant
    
    
    
    // ------------------------------------
    // -- TIME TO PLAY COMMERCIAL? --------
    // ------------------------------------
    
    
    
    function checkTime(e:TimerEvent) {
    		ns.pause();
    		liveVideoPlayer.btnStop.visible=false;
    		liveVideoPlayer.btnPlay.visible=true;
    		liveVideoPlayer.streamTitle.text="Playing Commercial";
    		playCommercial();
    }
    
    var t2:Timer = new Timer(90000);
    t2.addEventListener(TimerEvent.TIMER, checkTime);
    t2.start();
    
    
    
    // ------------------------------------
    // -- COMMERCIAL PLAYER ---------------
    // ------------------------------------
    
    function playCommercial():void {
    
    trace("Commercial started");
    
    var video2:Video=new Video(500,280);
    addChild(video2);
    video2.x=137;
    video2.y=10;
    
    var nc2:NetConnection = new NetConnection();
    nc2.connect(null);
    
    var ns2:NetStream=new NetStream(nc2);
    ns2.addEventListener(NetStatusEvent.NET_STATUS, onStatusEvent);
    
    function onStatusEvent(stat:Object):void {
    //trace(stat.info.code);
    if (stat.info.code=="NetStream.Play.Stop") {
    trace("Commercial ended");
    video2.visible=false;
    	ns.resume();
    	liveVideoPlayer.btnStop.visible=true;
    	liveVideoPlayer.btnPlay.visible=false;
    }
    }
    
    var meta:Object = new Object();
    meta.onMetaData = function(meta2:Object) {
    //trace(meta.duration);
    };
    
    ns2.client=meta;
    
    video2.attachNetStream(ns2);
    
    ns2.play("my_commercial_video_file_goes_here.flv");
    
    }

  4. #4
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    I could only think of 2 things that could cause this (and the first is just a guess):

    1) I think you only need 1 NetConnection, instead of 2, since it only "connects you to the net"

    2) you set "this" as client for the first NetStream, whereas you set the meta Object for the second one. Shouldn't it always be an Object?
    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

  5. #5
    Member
    Join Date
    Aug 2008
    Posts
    85
    I have to have have two NetConnections because 1 is connecting to a server, and one is connecting to null.

    Connect to (server ip)
    Play (livestream)
    Play (commercial.flv)

    Would result in the "commercial.flv" file not being found and not playing since it's not on the server. Likewise, if it connected to null, the commercial would play but the livestream would not.

    I will check your second suggestion. Such an inconsistency may very well be the problem.

    This is very eratic behavior. I now have a new FLA document with my chat movieclip (text area, list, text input, color picker) and actionscript, a plain stream (not my fancy player) and the commercials, all done in actionscript. I added a trace to every function and have done "Test Movie" about 2 dozen times now and it seems at various times that getMessages(); is called, with consistency, it pauses the video. I watch the "Output" window and as soon as it says "getMessages was called" (my trace text), it pauses. And for the record, when I say "pause" I mean "stop" but I don't like to say "stop" because that is different. If I had buttons to control the commercial, I believe I could resume the commercial. It's not like its just lagging for a second and resumes.. when it pauses... it stays paused.

    It seems something here is causing it. This is inside my getMessages function. This is my exact code, with the url replaced.

    Code:
    	var messageLoader:URLLoader = new URLLoader();
    	var messageData:URLRequest=new URLRequest("http://www.mysite.com/getmessages.php?usr="+x_username);
    
    	messageLoader.load(messageData);
    	messageLoader.addEventListener(Event.COMPLETE, messageLoadComplete);
    
    	function messageLoadComplete(event:Event):void {
    		chatroom.chatMessages.htmlText=messageLoader.data;
    	}

  6. #6
    Member
    Join Date
    Aug 2008
    Posts
    85
    I've now had the document I mentioned in my post above open for about 10 minutes with the getMessages function doing nothing but issuing a trace saying "getMessage was called". It has been working great, but now it seems as time has gone by, that the commercial pauses (AND RESUMES, different from before where it would stay paused) for about 1/8 of a second every time that the function is called.

    This is very weird. It's like the constant calling of the function is overloading the player or something. Like it can only do one thing at a time. Either play the video or do the function, but not both. Maybe the function with a trace doesn't have as much of an impact, but the function w/ the loading of the URL to get the text has a much larger impact, resulting in the latter causing the pausing much sooner and more severly.

    I'm at a loss. I'm going to look in to the "this" client thing you mentioned now.

  7. #7
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    perhaps the URLRequest temporarily interrupts the streaming because the Flash Player can only handle a certain amount of connections or requests?

    Maybe raising the buffer could solve this, since it can than play even with the stream being interrupted for a brief moment?

    Another way to get around this (hopefully) is to use this:
    PHP Code:
    ns.resume(); 
    which will unpause the NetStream when paused (I'm not sure what it does when it's playing, but I'm guessing not much )
    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

  8. #8
    Member
    Join Date
    Aug 2008
    Posts
    85
    The problem with that theory is that the pausing still occurs even if the function only contains a simple trace (no urlrequest). The setInterval is calling the function getMessages every 3 seconds, and sooner or later it will pause the video.

    I set the buffer for the flv player to 3 seconds and it didn't make any difference. The file it is loading is coming from my hard drive (while I build this) so network congestion isn't present. The entire 75 second commercial.flv file is loaded nearly instantly. This may indeed be a problem when this app goes on the internet, for which I will then deal with the buffering.

    And the ns.resume(); would seem like a "defensive" fix rather than an "offensive" fix. I'd rather prevent this issue than to try to play catch up and fix it every few seconds. I haven't tried this code for this reason, but I would assume it would still pause the video regardless, although the duration of the pause may be signifigantly smaller.

    I made a new document with a live player and the commercial player only, and it has been running for an hour without any issues. As soon as I throw in setInterval's, everything goes to hell.

    The attached "test2.fla" is a working example. Note that the server and flv are not mine. Also note that I get these errors regardless of if I load the .flv locally or if I load it from online. The commercial plays after 70 seconds, since the commercial is 60 seconds long. Once the commercial has played, it will play after 10 seconds of the live stream. This is a bug but I will fix it later.

    Now, if you add this code, it goes bezerk and pauses for like 30 seconds and then continues, or stops alltogether. It may take a few minutes but it will happen sooner or later.

    Code:
    function getMessages() {
    	trace("getMessages called");
    }
    
    getMessages();
    setInterval(getMessages, 1000);//1 sec
    Attached Files Attached Files
    Last edited by sufire; 08-02-2009 at 12:23 AM.

  9. #9
    Junior Member
    Join Date
    Sep 2009
    Posts
    1
    I had a similar problem except that load(urlRequest) was causing my flv to stop.

    With the problematic code, I was instantiating the netStream variable inside a function. When I made the netStream a class variable the problem went away. I'm not sure if this helps you who aren't using a Document class.

  10. #10
    Junior Member
    Join Date
    Dec 2009
    Posts
    1
    Hi,

    we've got similar problems with our flash movie. We're using two FLV playback components to play already fully loaded FLVs. We're doing a lot of jumping between cuePoints on that movie. But that is not an issue.

    But occassionally one of our movie also stops playing. In this case, the VideoEvent.STOPPED is thrown, so we are able to restart the movie again. But it's not a nice solution.

    Did anyone found a solution for this "bug"? We're using a lot of setTimeouts in our Actionscript code. If this is the reason, I currently consider changing our scripts to use custom events instead of setTimeouts. But as this would be a lot of work, I would be happy if a) anybody found another solution for this, or if b) anyone already tried with custom Events, but the bug still occurs?

    Unfortunately we cannot reproduce the bug on our site, we just get some mails from customers who face this behaviour.

    Kind regards
    Chris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center