A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [RESOLVED] Play random flv videos

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    15

    resolved [RESOLVED] Play random flv videos

    Hi,

    I want to play random videos after 10 seconds if the mouse has not been moved.


    I am getting this error:

    Error #1063: Argument count mismatch on video_fla::img_1/playvideo(). Expected 0, got 1.
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()
    Here is the code:

    var flvA:Array=["video1.flv","videos.flv","video3.flv"];

    var t:Timer=new Timer(5000,1);
    t.addEventListener(TimerEvent.TIMER,playvideo);

    this.addEventListener(MouseEvent.MOUSE_MOVE,mousem oved);

    function mousemoved(e:MouseEvent):void{
    t.reset();
    t.start();
    }

    var video:Video = new Video();
    addChild(video);

    var nc:NetConnection = new NetConnection();
    nc.connect(null);

    var ns:NetStream = new NetStream(nc);
    ns.client = {onMetaData:ns_onMetaData, onCuePoint:ns_onCuePoint};

    video.attachNetStream(ns);

    function playvideo():void{
    this.removeEventListner(MouseEvent.MOUSE_MOVE,mous emoved);
    t.removeEventListener(TimerEvent.TIMER,playvideo);
    ns.play(flvA[Math.floor(Math.random()*3)]);
    }

    function ns_onMetaData(item:Object):void {
    trace("metaData");
    video.width = item.width;
    video.height = item.height;
    video.x = (stage.stageWidth - video.width) / 2;
    video.y = (stage.stageHeight - video.height) / 2;
    }
    function ns_onCuePoint(item:Object):void {
    trace("cuePoint");
    trace(item.name + "\t" + item.time);
    }
    any ideas why I am getting the error?

    thanks

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    This line
    ns.play(flvA[Math.floor(Math.random()*3)]);
    is the problem. The play function has no parameters.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    15
    I'm not sure whats missing? please can you help/guide?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    In addition to what cancerinform says, the direct cause of the error is that your playvideo function takes no arguments. But you are using it as an event listener, so it will be passed 1 argument, the event.

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    15
    what would be the best way to solve the problem?
    any guidance/help is much appreciated.
    thank you

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    To solve the problem I mentioned
    Code:
    function playvideo():void{
    needs to be:
    Code:
    function playvideo(evt:Event):void{

  7. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    15
    thanks 5tons!!! that got rid of the Expected 0, got 1. and it played a video after 10 seconds! but it only played once.

    I guess this line will need tweaking to play a random video every 10 seconds the mouse has been inactive (rather than just the 1 time):

    ns.play(flvA[Math.floor(Math.random()*3)]);

    hope someone can put me in the right direction again!

    many thanks again

  8. #8
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    I made a mistake. play for ns has of course one argument

    This may solve your other problem:

    ns.addEventListener (NetStatusEvent.NET_STATUS, reportStatus);
    function reportStatus (event:NetStatusEvent):void
    {
    if (event.info.code == "NetStream.Buffer.Empty")
    {
    t.addEventListener (TimerEvent.TIMER,playvideo);
    t.start ();
    }
    }
    - The right of the People to create Flash movies shall not be infringed. -

  9. #9
    Junior Member
    Join Date
    Mar 2011
    Posts
    15
    cancerinform, thank you so much for the code!
    it plays a random video every 10 seconds. I have been trying to tweak the code to stop the videos playing if the mouse is active, but I am struggling.
    can you help me further please?

    many thanks again

  10. #10
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Here is a working script:
    PHP Code:
    import flash.events.NetStatusEvent;

    var 
    flvA:Array = ["video1.flv","video2.flv","video3.flv"];
    var 
    counter:int 0;
    var 
    t:Timer = new Timer(1000);
    t.addEventListener (TimerEvent.TIMER,playvideo);
    t.start ();

    stage.addEventListener (MouseEvent.MOUSE_MOVE,mousemoved);

    function 
    mousemoved (e:MouseEvent):void
    {
        
    counter 0;
    }

    var 
    video:Video = new Video();
    addChild (video);

    var 
    nc:NetConnection = new NetConnection();
    nc.connect (null);

    var 
    ns:NetStream = new NetStream(nc);
    ns.client 
    {
    onMetaData:ns_onMetaData,
    onCuePoint:ns_onCuePoint
    };
    ns.addEventListener (NetStatusEvent.NET_STATUSreportStatus);
    function 
    reportStatus (event:NetStatusEvent):void
    {
        if (
    event.info.code == "NetStream.Buffer.Empty")
        {
            
    t.start ();
            
    counter 0;
        }
    }
    video.attachNetStream (ns);

    function 
    playvideo (event:TimerEvent):void
    {
        if(
    counter >= 10)
        {
            
    t.stop ();
            
    ns.play (flvA[Math.floor(Math.random() * 3)]);
        }
        
    counter++;
    }

    function 
    ns_onMetaData (item:Object):void
    {
        
    trace ("metaData");
        
    video.width item.width;
        
    video.height item.height;
        
    video.= (stage.stageWidth video.width) / 2;
        
    video.= (stage.stageHeight video.height) / 2;
    }
    function 
    ns_onCuePoint (item:Object):void
    {
        
    trace ("cuePoint");
        
    trace (item.name "\t" item.time);

    - The right of the People to create Flash movies shall not be infringed. -

  11. #11
    Junior Member
    Join Date
    Mar 2011
    Posts
    15
    cancerinform... can't thank you enough!
    that sorted it! many thanks!

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