33 Replies Latest reply: Dec 29, 2013 7:42 PM by TavaresEd RSS

    Load a flv file from the net.How?

    TavaresEd Community Member

      Hi!

       

      I need to create a video player that will load the FLV file from the net.Can Anyone please give me an example of how do I do it?

       

      I've tried a lot of codes but none of them worked for me.

        • 1. Re: Load a flv file from the net.How?
          kglad CommunityMVP

          use your flv's url to assign the source of your flvplayback component or add to the play method of your netstream class.

          • 2. Re: Load a flv file from the net.How?
            TavaresEd Community Member

            Hi!

             

            This is the code I am using but it is returning an error message saying " Error opening URL 'http://www.djchambinho.com/Videos/PrimeiraQuinta.flv' NetStream.Play.StreamNotFound

             

             

             

            var video:Video = new Video(320, 240);

            addChild(video);

             

            var nc:NetConnection = new NetConnection();

            nc.connect(null);

             

            var ns:NetStream = new NetStream(nc);

            ns.addEventListener(NetStatusEvent.NET_STATUS, onStatusEvent);

             

             

            function onStatusEvent(stat:Object):void

            {

                      trace(stat.info.code);

            }

             

             

            var meta:Object = new Object();

            meta.onMetaData = function(meta:Object)

            {

                      trace(meta.duration);

            }

             

             

            ns.client = meta;

             

             

            video.attachNetStream(ns);

             

             

            ns.play("http://www.djchambinho.com/Videos/PrimeiraQuinta.flv");

             

            thank you

            • 3. Re: Load a flv file from the net.How?
              kglad CommunityMVP

              your url is incorrect.

              • 4. Re: Load a flv file from the net.How?
                TavaresEd Community Member

                why is it wrong? that is where my flv is located.

                What is the correct URL then?

                • 5. Re: Load a flv file from the net.How?
                  TavaresEd Community Member

                  I've tried this code too that I got from flash help adobe's site and it is not working eighter.I've changed the URL and it is saying that StreamNotFound but if I dont change the url it works fine.

                   

                  package {

                      import flash.display.Sprite;

                      import flash.events.NetStatusEvent;

                      import flash.events.SecurityErrorEvent;

                      import flash.media.Video;

                      import flash.net.NetConnection;

                      import flash.net.NetStream;

                      import flash.events.Event;

                   

                      public class NetConnectionExample extends Sprite {

                          private var videoURL:String = "http://www.helpexamples.com/flash/video/cuepoints.flv";

                          private var connection:NetConnection;

                          private var stream:NetStream;

                          private var video:Video = new Video();       

                   

                          public function NetConnectionExample() {

                              connection = new NetConnection();

                              connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

                              connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);

                              connection.connect(null);

                          }

                   

                          private function netStatusHandler(event:NetStatusEvent):void {

                              switch (event.info.code) {

                                  case "NetConnection.Connect.Success":

                                      connectStream();

                                      break;

                                  case "NetStream.Play.StreamNotFound":

                                      trace("Stream not found: " + videoURL);

                                      break;

                              }

                          }

                   

                          private function securityErrorHandler(event:SecurityErrorEvent):void {

                              trace("securityErrorHandler: " + event);

                          }

                   

                          private function connectStream():void {

                              addChild(video);

                              var stream:NetStream = new NetStream(connection);

                              stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

                              stream.client = new CustomClient();

                              video.attachNetStream(stream);

                              stream.play(videoURL);

                          }

                      }

                  }

                   

                  class CustomClient {

                      public function onMetaData(info:Object):void {

                          trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);

                      }

                      public function onCuePoint(info:Object):void {

                          trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);

                      }

                  }

                  • 6. Re: Load a flv file from the net.How?
                    kglad CommunityMVP

                    it's wrong because when i click it i see 404 error instead of the flv.  i don't know the correct url because i can't inspect your directories.  i can see that you have a Videos directory but i can't see what's in it.

                    • 7. Re: Load a flv file from the net.How?
                      TavaresEd Community Member

                      but the flv file is inside this directory and this is the correct path.I don't know it is not working? The file has 197MB.Do you think this might be the problem?

                      • 8. Re: Load a flv file from the net.How?
                        kglad CommunityMVP

                        the code in message 5 works for me.

                         

                        if you change videoURL to "http://www.djchambinho.com/Videos/PrimeiraQuinta.flv" i would expect the message 5 code to fail for the same reason as your message 2 code:  the url is wrong.

                         

                        you could attach a screenshot of the http://www.djchambinho.com/Videos/ directory to get more specific help about why your url is wrong.

                        • 9. Re: Load a flv file from the net.How?
                          kglad CommunityMVP

                          no, using a 197mb flv won't cause that problem.  using a large flv can cause problems but not the problem you're seeing.

                          • 10. Re: Load a flv file from the net.How?
                            TavaresEd Community Member

                            the code on message 5 worked if I don't change the URL....if I change it I get the streamnotfound message.

                             

                            Screen Shot 2013-12-24 at 2.50.47 PM.png

                            • 11. Re: Load a flv file from the net.How?
                              kglad CommunityMVP

                              case counts online.  fix your case problems.

                               

                              also, if your swf is in that root directory, use a relative path:  "videos/priimeiraquinta.fla"

                              • 12. Re: Load a flv file from the net.How?
                                TavaresEd Community Member

                                I've already changed Videos for videos and PrimeiraQuinta for primeiraquinta and changed my code. It did not work. my swf is on my computer yet..I did not upload it yet.But with the url I got from the exemple it worked fine.

                                • 13. Re: Load a flv file from the net.How?
                                  kglad CommunityMVP

                                  click that refresh button on your ftp client to make sure that flv is still in videos and still has the same name.

                                  • 14. Re: Load a flv file from the net.How?
                                    TavaresEd Community Member

                                    I clicked but it didn't work.

                                    • 15. Re: Load a flv file from the net.How?
                                      kglad CommunityMVP

                                      what do you mean, it didn't work.

                                       

                                      did the refresh button show your flv is still on your server in your videos directory?

                                      • 16. Re: Load a flv file from the net.How?
                                        TavaresEd Community Member

                                        Yes, I clicked the refresh button and the flv file is in there...

                                        • 17. Re: Load a flv file from the net.How?
                                          kglad CommunityMVP

                                          you could check your server's mime types to make sure it knows how to handle flv files.

                                          • 18. Re: Load a flv file from the net.How?
                                            TavaresEd Community Member

                                            How do I do this?

                                            • 19. Re: Load a flv file from the net.How?
                                              TavaresEd Community Member

                                              I've added .flv extension and it worked.Now I need to make a preloader for this video because it is taking too long. How do I do this preloader?

                                               

                                              thank you very much!

                                              • 20. Re: Load a flv file from the net.How?
                                                TavaresEd Community Member

                                                it's funny.When I run it on a computer it works but if I run it on my samsung Android phone it stops working....My app freezes and closes..Dark screen.After I open my app acouple of times I even here some sound but then it stops and doesnt show anyting..if I close my app and open it again then I see the app but no video and no sound

                                                 

                                                After sometime it is playing but the video has transparence ...

                                                 

                                                I guess I need to make a preloader or something but I dont know because once I click a button to open the swf file into an empty MC the screen gets dark

                                                • 21. Re: Load a flv file from the net.How?
                                                  kglad CommunityMVP

                                                  start the stream and immediately pause it.  use a loop to track the bytesLoaded property of your netstream.

                                                   

                                                  when enough video has loaded to ensure smooth playback, unpause the stream.

                                                   

                                                  and the problems in you android might be because that video is so large.  try testing with a smaller sized flv.

                                                  • 22. Re: Load a flv file from the net.How?
                                                    TavaresEd Community Member

                                                    is there a method I have to do this loop inside or just make a loop after I put ns.pause()? How would I do this loop?

                                                    I have upload a smaller size file to a server and still I get a black screen.

                                                    • 23. Re: Load a flv file from the net.How?
                                                      kglad CommunityMVP

                                                      use a timer or enterframe loop.

                                                       

                                                      make sure you grant internet privileges for your android app.

                                                      • 24. Re: Load a flv file from the net.How?
                                                        TavaresEd Community Member

                                                        I'm lost in this timer thing. is it something like this?

                                                         

                                                        var t : Timer = new Timer(ns.bytesTotal,0);

                                                        t.addEventListener("timer",timeHandler);

                                                        t.start();

                                                         

                                                         

                                                        function timeHandler(event : TimeEvent) : void

                                                        {

                                                        // how do I check here to resume the playback

                                                        }

                                                        • 25. Re: Load a flv file from the net.How?
                                                          kglad CommunityMVP

                                                          like this,

                                                           

                                                          // define your netstream, assign its play() and pause().

                                                          var t:Timer=new Timer(1000,0);

                                                          t.addEventListener(TimerEvent.TIMER,timerHandler);

                                                          t.start();

                                                           

                                                          function timerHandler(e:TimerEvent):void{

                                                          trace(ns.bytesLoaded,ns.bytesTotal);

                                                          // terminate loop (remove listener) when no longer needed

                                                          }

                                                          • 26. Re: Load a flv file from the net.How?
                                                            TavaresEd Community Member

                                                            I've done this but it is still freezing when i test inside my android phone..

                                                             

                                                            import flash.utils.Timer;

                                                            import flash.events.TimerEvent;

                                                             

                                                             

                                                            var video:Video = new Video(320, 240);

                                                            addChild(video);

                                                             

                                                             

                                                            video.x = 80;

                                                            video.y = 100;

                                                             

                                                            var nc:NetConnection = new NetConnection();

                                                            nc.connect(null);

                                                             

                                                            var ns:NetStream = new NetStream(nc);

                                                            ns.addEventListener(NetStatusEvent.NET_STATUS, onStatusEvent);

                                                            ns.bufferTime = 1;

                                                             

                                                             

                                                            function onStatusEvent(stat:Object):void

                                                            {

                                                                      trace(stat.info.code);

                                                            }

                                                             

                                                             

                                                            var meta:Object = new Object();

                                                            meta.onMetaData = function(meta:Object)

                                                            {

                                                                      trace(meta.duration);

                                                             

                                                             

                                                            }

                                                             

                                                            ns.client = meta;

                                                            video.attachNetStream(ns);

                                                             

                                                            ns.play("http://www.djchambinho.com/Videos/primeiraquinta.flv");

                                                            ns.pause();

                                                             

                                                             

                                                            var t : Timer = new Timer(1000,0);

                                                            var nsBL : Number = ns.bytesLoaded;

                                                            var nsBT : Number = ns.bytesTotal;

                                                            var perc : Number;

                                                             

                                                             

                                                            t.addEventListener(TimerEvent.TIMER,timeHandler);

                                                            t.start();

                                                             

                                                             

                                                            function timeHandler(event : TimerEvent) : void

                                                            {

                                                             

                                                              if (ns.bytesLoaded == ns.bytesTotal )

                                                              {

                                                                        ns.resume();

                                                                        removeEventListener(TimerEvent.TIMER,timeHandler);

                                                                        t.stop();

                                                              }

                                                            }

                                                            • 27. Re: Load a flv file from the net.How?
                                                              kglad CommunityMVP

                                                              again, make sure you have permissions and use:

                                                               

                                                               

                                                               

                                                              import flash.utils.Timer;

                                                              import flash.events.TimerEvent;

                                                               

                                                               

                                                              var video:Video = new Video(320, 240);

                                                              addChild(video);

                                                               

                                                               

                                                              video.x = 80;

                                                              video.y = 100;

                                                               

                                                              var nc:NetConnection = new NetConnection();

                                                              nc.connect(null);

                                                               

                                                              var ns:NetStream = new NetStream(nc);

                                                              ns.addEventListener(NetStatusEvent.NET_STATUS, onStatusEvent);

                                                              ns.bufferTime = 1;

                                                               

                                                               

                                                              function onStatusEvent(stat:Object):void

                                                              {

                                                                        trace(stat.info.code);

                                                              }

                                                               

                                                               

                                                              var meta:Object = new Object();

                                                              meta.onMetaData = function(meta:Object)

                                                              {

                                                                        trace(meta.duration);

                                                               

                                                               

                                                              }

                                                               

                                                              ns.client = meta;

                                                              video.attachNetStream(ns);

                                                               

                                                              ns.play("http://www.djchambinho.com/videos/primeiraquinta.flv");

                                                              ns.pause();

                                                               

                                                               

                                                              var t : Timer = new Timer(1000,0);

                                                              var nsBL : Number = ns.bytesLoaded;

                                                              var nsBT : Number = ns.bytesTotal;

                                                              var perc : Number;

                                                               

                                                               

                                                              t.addEventListener(TimerEvent.TIMER,timeHandler);

                                                              t.start();

                                                               

                                                               

                                                              function timeHandler(event : TimerEvent) : void

                                                              {

                                                               

                                                                if (ns.bytesLoaded>0&&ns.bytesLoaded == ns.bytesTotal )

                                                                {

                                                                          ns.resume();

                                                                          t.removeEventListener(TimerEvent.TIMER,timeHandler);

                                                                          t.stop();

                                                                }

                                                              }

                                                              • 28. Re: Load a flv file from the net.How?
                                                                TavaresEd Community Member

                                                                Funny...I am doind this..My main app video player.apk has 3 buttons that I load an external swf file into an empty MC.When I click the button to open a swf file with the code to play a video(the one we are talking about previously) it freezes and does not open the video...but if I publish only the swf file with the video it works fine.

                                                                 

                                                                what is wrong in calling an external swf file into my main app

                                                                • 29. Re: Load a flv file from the net.How?
                                                                  kglad CommunityMVP

                                                                  explain your setup more carefully.

                                                                  • 30. Re: Load a flv file from the net.How?
                                                                    TavaresEd Community Member

                                                                    I have a main movie (.swf) that has 3 buttons.One button when I click brings the user to the main screen.Another button that when user clicks gets him to the swf file with the video(the code we've been working on) and another button that I will take to another swf file that shows a live broadcast(that other code I've sent you to help me).

                                                                     

                                                                    This is the code I have for the main swf file.

                                                                     

                                                                    import flash.events.MouseEvent;

                                                                     

                                                                     

                                                                    preloaderBar.visible = false;

                                                                    var loader:Loader = new Loader();

                                                                    btHome.enabled = false;

                                                                     

                                                                     

                                                                    carregaFilme("home.swf");

                                                                     

                                                                     

                                                                     

                                                                     

                                                                    function carregaFilme(filme : String ) :void

                                                                    {

                                                                      var reqMovie:URLRequest = new URLRequest(filme);

                                                                      loader.load(reqMovie);

                                                                     

                                                                      loader.contentLoaderInfo.addEventListener(Event.OPEN,comeco);

                                                                      loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progresso);

                                                                      loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completo);

                                                                     

                                                                      palco.addChild(loader); 

                                                                    }

                                                                     

                                                                     

                                                                    function comeco(event:Event):void

                                                                    {

                                                                              preloaderBar.visible = true;

                                                                              preloaderBar.barra.scaleX = 0;

                                                                    }

                                                                     

                                                                     

                                                                    function progresso(e:ProgressEvent):void

                                                                    {

                                                                              var perc:Number = e.bytesLoaded / e.bytesTotal;

                                                                              preloaderBar.percent.text = Math.ceil(perc*100).toString();

                                                                              preloaderBar.barra.scaleX =  perc;

                                                                    }

                                                                     

                                                                     

                                                                    function completo(e:Event):void

                                                                    {

                                                                              preloaderBar.percent.text = '';

                                                                              preloaderBar.visible = false;

                                                                    }

                                                                     

                                                                     

                                                                    btHome.addEventListener(MouseEvent.MOUSE_DOWN,onHomeDown);

                                                                    btHome.addEventListener(MouseEvent.MOUSE_UP,onHomeUp);

                                                                    btSets.addEventListener(MouseEvent.MOUSE_DOWN,onSetsDown);

                                                                    btSets.addEventListener(MouseEvent.MOUSE_UP,onSetsUp);

                                                                    btVivo.addEventListener(MouseEvent.MOUSE_DOWN,onVivoDown);

                                                                    btVivo.addEventListener(MouseEvent.MOUSE_UP,onVivoUp);

                                                                    btHome.addEventListener(MouseEvent.CLICK,onHomeClick);

                                                                    btSets.addEventListener(MouseEvent.CLICK,onSetsClick);

                                                                     

                                                                     

                                                                    function onSetsClick(Event : MouseEvent) : void

                                                                    {

                                                                      carregaFilme("sets.swf");

                                                                    }

                                                                     

                                                                     

                                                                    function onHomeClick(Event : MouseEvent) : void

                                                                    {

                                                                              carregaFilme("home.swf");

                                                                    }

                                                                     

                                                                     

                                                                     

                                                                     

                                                                    function onHomeDown(Event : MouseEvent) : void

                                                                    {

                                                                              btHome.y += 1;

                                                                    }

                                                                     

                                                                     

                                                                    function onHomeUp(Event : MouseEvent) : void

                                                                    {

                                                                              btHome.y -= 1;

                                                                    }

                                                                     

                                                                     

                                                                    function onSetsDown(Event : MouseEvent) : void

                                                                    {

                                                                              btSets.y += 1;

                                                                    }

                                                                     

                                                                     

                                                                    function onSetsUp(Event : MouseEvent) : void

                                                                    {

                                                                              btSets.y -= 1;

                                                                    }

                                                                     

                                                                     

                                                                    function onVivoDown(Event : MouseEvent) : void

                                                                    {

                                                                              btVivo.y += 1;

                                                                    }

                                                                     

                                                                     

                                                                    function onVivoUp(Event : MouseEvent) : void

                                                                    {

                                                                              btVivo.y -= 1;

                                                                    }

                                                                     

                                                                    when I click btSets is when I get a black screen.but if I publish the sets.swf file to be a android app it works fine.

                                                                    • 31. Re: Load a flv file from the net.How?
                                                                      kglad CommunityMVP

                                                                      make sure the main app requests internet access and use:

                                                                       

                                                                      import flash.utils.Timer;

                                                                      import flash.events.TimerEvent;

                                                                       

                                                                      var video:Video;

                                                                      var nc:NetConnection;

                                                                      var ns:NetStream;

                                                                      var t : Timer = new Timer(1000,0);

                                                                      var meta:Object = new Object();

                                                                       

                                                                      this.addEventListener(Event.ADDED_TO_STAGE,init);

                                                                       

                                                                      function init(e:Event):void{

                                                                      video= new Video(320, 240);

                                                                      addChild(video);

                                                                      video.x = 80;

                                                                      video.y = 100;

                                                                       

                                                                      nc= new NetConnection();

                                                                      nc.connect(null);

                                                                      ns = new NetStream(nc);

                                                                      ns.addEventListener(NetStatusEvent.NET_STATUS, onStatusEvent);

                                                                      ns.bufferTime = 1;

                                                                       

                                                                      ns.client = meta;

                                                                      video.attachNetStream(ns);

                                                                       

                                                                      ns.play("http://www.djchambinho.com/videos/primeiraquinta.flv");

                                                                      ns.pause();

                                                                       

                                                                      t.addEventListener(TimerEvent.TIMER,timeHandler);

                                                                      t.start();

                                                                      }

                                                                       

                                                                      function onStatusEvent(stat:Object):void

                                                                      {

                                                                                trace(stat.info.code);

                                                                      }

                                                                       

                                                                      meta.onMetaData = function(meta:Object)

                                                                      {

                                                                                trace(meta.duration);

                                                                      }

                                                                       

                                                                       

                                                                      function timeHandler(event : TimerEvent) : void

                                                                      {

                                                                       

                                                                        if (ns.bytesLoaded>0&&ns.bytesLoaded == ns.bytesTotal )

                                                                        {

                                                                                  ns.resume();

                                                                                  t.removeEventListener(TimerEvent.TIMER,timeHandler);

                                                                                  t.stop();

                                                                        }

                                                                      }

                                                                      • 32. Re: Load a flv file from the net.How?
                                                                        TavaresEd Community Member

                                                                        Now it opens sets.swf but the video doesnt start even testing it on a computer

                                                                        • 33. Re: Load a flv file from the net.How?
                                                                          TavaresEd Community Member

                                                                          any ideas?