11 Replies Latest reply on Mar 19, 2010 9:59 AM by DigitalArchitectCanada

    Flash AIR MP3 Player Issue

    el pescado

      Hello,

       

      I thought I had this down, but does not seem to be working.....


      I am attempting to create an Flash AIR app using the code below.  I have used this code in the past and it works fine.  I am keeping 4 audio files (in a folder called "audio") and an XML file out on my server so I can switch out music and track names once distributed.

       

      It works PERFECT when I run it as a Flash file.  When I publish it as an AIR app, install it........the text works fine BUT I hear no music.

       

      Any suggestions?

       


      Code:

       

      var my_songs:XMLList;

      var my_total:Number;

       

      var my_sound:Sound;

      var my_channel:SoundChannel;

       

      var current_song:Number = 0;

       

      var song_position:Number;

      var song_paused:Boolean;

       

      var myXMLLoader:URLLoader = new URLLoader();

      myXMLLoader.load(new URLRequest("http://myurl/playlist.xml"));

      myXMLLoader.addEventListener(Event.COMPLETE, processXML);

       

      function processXML(e:Event):void {

      var myXML:XML = new XML(e.target.data);

       

      my_songs = myXML.SONG;

      my_total = my_songs.length();

       

      //playSong(0);

       

      myXMLLoader.removeEventListener(Event.COMPLETE, processXML);

      myXMLLoader = null;

       

      }

       

      function playSong(mySong:Number):void {

      var myTitle = my_songs[mySong].@TITLE;

      var myArtist = my_songs[mySong].@ARTIST;

      var myURL = my_songs[mySong].@URL;

       

      title_txt.text = myTitle;

      artist_txt.text = myArtist;

       

      if (my_channel) {

      my_channel.stop();

      my_channel.removeEventListener(Event.SOUND_COMPLETE, onNext);

      }

       

      my_sound = new Sound();

      my_sound.load(new URLRequest(myURL));

      my_channel = my_sound.play();

      my_channel.addEventListener(Event.SOUND_COMPLETE, onNext);

      }

       

      next_btn.addEventListener(MouseEvent.CLICK, onNext);

      function onNext(e:Event):void {

      current_song++;

      if (current_song>=my_total) {

      current_song=0;

      }

      playSong(current_song);

      }

      prev_btn.addEventListener(MouseEvent.CLICK, onPrev);

      function onPrev(e:MouseEvent):void {

      current_song--;

      if (current_song<0) {

      current_song = my_total-1;

      }

      playSong(current_song);

      }

       

      pause_btn.addEventListener(MouseEvent.CLICK, onPause);

      function onPause(e:MouseEvent):void {

      if (my_channel) {

      song_position = my_channel.position;

      my_channel.stop();

      song_paused=true;

      }

      }

      play_btn.addEventListener(MouseEvent.CLICK, onPlay);

      function onPlay(e:MouseEvent):void {

      if (song_paused) {

      my_channel = my_sound.play(song_position);

      song_paused=false;

      } else if (!my_channel) {

      playSong(current_song);

      }

      }

        • 1. Re: Flash AIR MP3 Player Issue
          kglad Adobe Community Professional & MVP

          add an ioerror listener to your sound to see if there's a security sandbox issue.  make sure you can see if there's an error.

          • 2. Re: Flash AIR MP3 Player Issue
            el pescado Level 1

            I am a bit lost on that one........need a few more hints.

            • 3. Re: Flash AIR MP3 Player Issue
              el pescado Level 1

              Like this?

               

              my_sound.addEventListener(IOErrorEvent.IO_ERROR, soundIOErrorHandler);

              function soundIOErrorHandler(event:IOErrorEvent)

              {

               

              }

              • 4. Re: Flash AIR MP3 Player Issue
                kglad Adobe Community Professional & MVP

                yes.  now add a trace() in your listener function (if you're able to see trace output using air) or use a textfield.

                • 5. Re: Flash AIR MP3 Player Issue
                  el pescado Level 1

                  The thing is, I am not getting an error of any kind.  If I publish as a swf it work perfect (calling out to the server for the XML file and audio).

                   

                  If i publish it as an AIR app, install, the text fields work fine..........but no music.

                  • 6. Re: Flash AIR MP3 Player Issue
                    el pescado Level 1

                    Added this trace:

                     

                    my_sound.addEventListener(IOErrorEvent.IO_ERROR, soundIOErrorHandler);

                    function soundIOErrorHandler(event:IOErrorEvent) {

                    trace("works"); {

                    }

                    }

                     

                     

                     

                     

                     

                    Got this error:

                     

                     

                    [SWF] AS3%20Music%20Player.swf - 5358 bytes after decompression

                    TypeError: Error #1009: Cannot access a property or method of a null object reference.

                    at AS3MusicPlayer_fla::MainTimeline/frame1()[AS3MusicPlayer_fla.MainTimeline::frame1:83]

                    at runtime::ContentPlayer/loadInitialContent()

                    at runtime::ContentPlayer/playRawContent()

                    at runtime::ContentPlayer/playContent()

                    at runtime::AppRunner/run()

                    at global/runtime::ADLEntry()

                    • 7. Re: Flash AIR MP3 Player Issue
                      DigitalArchitectCanada Level 3

                      Your sound object has nothing in it, it's because the load isn't complete. You need to add an event listener to my_sound.load and then inside that, play the sound.

                       

                      var songRequest:URLRequest = new URLRequest();

                      songRequest.url = "the url in here";

                       

                      my_sound.addEventListener(Event.COMPLETE, playLoadedSong);

                      my_sound.load(songRequest);

                       

                      function playLoadedSong(e:Event):void

                      {

                           my_channel = my_sound.play();

                           my_channel.addEventListener(Event.SOUND_COMPLETE, onNext);
                      }

                      • 8. Re: Flash AIR MP3 Player Issue
                        el pescado Level 1

                        Thanks for the replay.......always appreciated.

                         

                        Like this you mean?  Sorry, about not picking this up.  I am still getting the error.  I am also confused why it works as Flash but not as the AIR app?

                         

                        var my_songs:XMLList;

                        var my_total:Number;

                         

                        var my_sound:Sound;

                        var my_channel:SoundChannel;

                         

                        var current_song:Number = 0;

                         

                        var song_position:Number;

                        var song_paused:Boolean;

                         

                        var myXMLLoader:URLLoader = new URLLoader();

                        myXMLLoader.load(new URLRequest("http://www.myURL.com/playlist.xml"));

                        myXMLLoader.addEventListener(Event.COMPLETE, processXML);

                         

                        function processXML(e:Event):void {

                        var myXML:XML = new XML(e.target.data);

                         

                        my_songs = myXML.SONG;

                        my_total = my_songs.length();

                         

                        playSong(0);

                         

                        myXMLLoader.removeEventListener(Event.COMPLETE, processXML);

                        myXMLLoader = null;

                         

                        }

                         

                        function playSong(mySong:Number):void {

                        var myTitle = my_songs[mySong].@TITLE;

                        var myArtist = my_songs[mySong].@ARTIST;

                        var myURL = my_songs[mySong].@URL;

                         

                        title_txt.text = myTitle;

                        artist_txt.text = myArtist;

                         

                        if (my_channel) {

                        my_channel.stop();

                        my_channel.removeEventListener(Event.SOUND_COMPLETE, onNext);

                        }

                        }

                        var songRequest:URLRequest = new URLRequest();

                        songRequest.url = "http://www.myURL.com/audio";

                         

                        my_sound.addEventListener(Event.COMPLETE, playLoadedSong);

                        my_sound.load(songRequest);

                         

                        function playLoadedSong(e:Event):void

                        {

                        my_channel = my_sound.play();

                             my_channel.addEventListener(Event.SOUND_COMPLETE, onNext);

                        }

                         

                         

                        next_btn.addEventListener(MouseEvent.CLICK, onNext);

                        function onNext(e:Event):void {

                        current_song++;

                        if (current_song>=my_total) {

                        current_song=0;

                        }

                        playSong(current_song);

                        }

                        prev_btn.addEventListener(MouseEvent.CLICK, onPrev);

                        function onPrev(e:MouseEvent):void {

                        current_song--;

                        if (current_song<0) {

                        current_song = my_total-1;

                        }

                        playSong(current_song);

                        }

                         

                        pause_btn.addEventListener(MouseEvent.CLICK, onPause);

                        function onPause(e:MouseEvent):void {

                        if (my_channel) {

                        song_position = my_channel.position;

                        my_channel.stop();

                        song_paused=true;

                        }

                        }

                        play_btn.addEventListener(MouseEvent.CLICK, onPlay);

                        function onPlay(e:MouseEvent):void {

                        if (song_paused) {

                        my_channel = my_sound.play(song_position);

                        song_paused=false;

                        } else if (!my_channel) {

                        playSong(current_song);

                        }

                        }

                        • 9. Re: Flash AIR MP3 Player Issue
                          DigitalArchitectCanada Level 3

                          I didn't see this line anywhere:

                           

                          my_sound = new Sound();

                           

                          should be right here:

                           

                          var songRequest:URLRequest = new URLRequest();

                          songRequest.url = "http://www.myURL.com/audio";


                          my_sound = new Sound();
                          my_sound.addEventListener(Event.COMPLETE, playLoadedSong);

                          my_sound.load(songRequest);

                          Let me know... and don't be sorry everyone is always learning something

                          • 10. Re: Flash AIR MP3 Player Issue
                            el pescado Level 1

                            Hey, thank you Jesse.

                             

                            Still getting the error.

                             

                            Attaching file with proper URLs.

                             

                            Any other suggestions would be greatly appreciated.  I want to learn how to properly send dynamic data through an AIR app. Text, songs and video.

                             

                            Thanks.

                             

                             

                             

                            CODE:

                             

                             

                            var my_songs:XMLList;

                            var my_total:Number;

                             

                            var my_sound:Sound;

                            var my_channel:SoundChannel;

                             

                            var current_song:Number = 0;

                             

                            var song_position:Number;

                            var song_paused:Boolean;

                             

                            var myXMLLoader:URLLoader = new URLLoader();

                            myXMLLoader.load(new URLRequest("http://www.stickybangbang.com/playlist.xml"));

                            myXMLLoader.addEventListener(Event.COMPLETE, processXML);

                             

                            function processXML(e:Event):void {

                            var myXML:XML = new XML(e.target.data);

                             

                            my_songs = myXML.SONG;

                            my_total = my_songs.length();

                             

                            playSong(0);

                             

                            myXMLLoader.removeEventListener(Event.COMPLETE, processXML);

                            myXMLLoader = null;

                             

                            }

                             

                            function playSong(mySong:Number):void {

                            var myTitle = my_songs[mySong].@TITLE;

                            var myArtist = my_songs[mySong].@ARTIST;

                            var myURL = my_songs[mySong].@URL;

                             

                            title_txt.text = myTitle;

                            artist_txt.text = myArtist;

                             

                            if (my_channel) {

                            my_channel.stop();

                            my_channel.removeEventListener(Event.SOUND_COMPLETE, onNext);

                            }

                            }

                             

                            var songRequest:URLRequest = new URLRequest();

                            songRequest.url = "http://www.stickybangbang.com/audio";

                             

                            my_sound = new Sound();

                            my_sound.addEventListener(Event.COMPLETE, playLoadedSong);

                            my_sound.load(songRequest);

                             

                            function playLoadedSong(e:Event):void

                            {

                            my_channel = my_sound.play();

                                 my_channel.addEventListener(Event.SOUND_COMPLETE, onNext);

                            }

                             

                             

                            next_btn.addEventListener(MouseEvent.CLICK, onNext);

                            function onNext(e:Event):void {

                            current_song++;

                            if (current_song>=my_total) {

                            current_song=0;

                            }

                            playSong(current_song);

                            }

                            prev_btn.addEventListener(MouseEvent.CLICK, onPrev);

                            function onPrev(e:MouseEvent):void {

                            current_song--;

                            if (current_song<0) {

                            current_song = my_total-1;

                            }

                            playSong(current_song);

                            }

                             

                            pause_btn.addEventListener(MouseEvent.CLICK, onPause);

                            function onPause(e:MouseEvent):void {

                            if (my_channel) {

                            song_position = my_channel.position;

                            my_channel.stop();

                            song_paused=true;

                            }

                            }

                            play_btn.addEventListener(MouseEvent.CLICK, onPlay);

                            function onPlay(e:MouseEvent):void {

                            if (song_paused) {

                            my_channel = my_sound.play(song_position);

                            song_paused=false;

                            } else if (!my_channel) {

                            playSong(current_song);

                            }

                            }

                            • 11. Re: Flash AIR MP3 Player Issue
                              DigitalArchitectCanada Level 3

                              Well heres the first problem,

                               

                              It works as a flash app on your website because the SWF is operating within the domain, so it can access world-only restricted files and directories. Your audio directory is restricted in this way, go ahead and click this:

                               

                              http://www.stickybangbang.com/audio/

                               

                              So when AIR makes the request, it gets denied the same way your browser is.

                               

                              Second thing have you traced out your XML attributes to make sure they're read properly?

                               

                              Third thing:

                               

                              I dont see where you're appending the names you're getting from the XML list to the song URLRequest. Also your URLRequest should not include /audio/ because it already exists in your XML.

                               

                              should be:

                               

                              var songRequest:URLRequest = new URLRequest();

                              songRequest.url  = "http://www.stickybangbang.com/" + my_songs[song number].attribute("URL");

                               

                              my_sound =  new Sound();

                              my_sound.addEventListener(Event.COMPLETE,  playLoadedSong);

                              my_sound.load(songRequest);

                               

                              If you want to restrict your stuff and have AIR play it, you're gonna need to do some PHP and hide your songs in a database or perhaps leave them in the folder and put the php script on the main URL like stickybangbang.com/getSong.php.

                               

                              The php script would either have to select the database blob and return it or select the file based on your request. So if you used a URLLoader and attached a song number to it, you could have PHP get that number and do some logic via POST.

                               

                              Like this:

                               

                              <?PHP

                              $theSongNumber = $_POST['varFromFlash'];

                               

                              if($theSongNumber == 0){

                                   return file_get_contents(file(../song.mp3));

                              }

                              ?>

                               

                              or something like that you'd have to google or I'd have to look into it more. You'd obviously still need to include a database lookup for user info because then anyone could just ping that script and get your files.

                               

                              Anyway maybe to keep it simple for now try removing the folder restrictions and testing. Let me know,

                               

                              Jesse