18 Replies Latest reply: Mar 12, 2013 7:48 AM by kglad RSS

    MP3 Ios External or Internal?

    Vendita3

      I have a Flash movie that plays MP3 files on a button click using this:

       

      function btnClick2(event:MouseEvent):void {

      //SoundMixer.stopAll();

      var mySound:Sound = new Sound();

      mySound.load(new URLRequest("MP3s/name.mp3"));

      mySound.play();

      }

       

      It works fine in publish preview, and it will work once published to the IPhone but it seems intermittant. It will not always play for each click. Sometimes double press. Just strange results.

       

      Am I better for IOs actually playing it from the local library (some how)?

       

      Or is there a better way to get this to work?

      Thanks for your help!

        • 1. Re: MP3 Ios External or Internal?
          kglad CommunityMVP

          yes, it would be better to play it from your local library because it will play without a momentary delay that you would expect with loading from iOS. 

           

          import the mp3 to your library, right click it, click properties, tick export for actionscript and assign a class name (eg, MP3).

           

          you can then use:

           

           

          function btnClick2(event:MouseEvent):void {

          //SoundMixer.stopAll();

          var mySound:Sound = new MP3();

          mySound.play();

          }

          • 2. Re: MP3 Ios External or Internal?
            Vendita3 Community Member

            OK - excellent.

             

            Thanks very much. I will update them all.

            • 3. Re: MP3 Ios External or Internal?
              Vendita3 Community Member

              Hi - OK stuck again. I can get this working with how you suggested no probs.

              But I have a load of buttons all using the same script. Previously I was using this:

               

              function btnClick2(event:MouseEvent):void {

              var mySound:Sound = new Sound();

              mySound.load(new URLRequest("MP3s/" + event.target.name + ".mp3"));

              mySound.play();

              }

              PlayAMajor.addEventListener(MouseEvent.CLICK, btnClick2);

              PlayBMajor.addEventListener(MouseEvent.CLICK, btnClick2);

               

              I have tried to use something similar but I can not get it to work:

               

               

              function btnClick2(event:MouseEvent):void {

              var mySound:Sound = new event.target.name+"mp3"();

              mySound.play();

              }

              AString.addEventListener(MouseEvent.CLICK, btnClick2);

               

              Any idea how I can make this line dynamic: var mySound:Sound = new event.target.name+"mp3"();

               

              So the button will be the same property as the MP3 but the MP3 we just add "mp3" (if that makes sense!)

              • 4. Re: MP3 Ios External or Internal?
                kglad CommunityMVP

                use:

                 

                 

                assign your sounds class names like whatever_MP3 where whatever matches your button names.  then use:

                 

                 

                function btnClick2(event:MouseEvent):void {

                var C:Class=Class(getDefinitionByName(event.currentTarget.name+"_MP3"));

                var c:Sound = new C();

                c.play();

                }

                AString.addEventListener(MouseEvent.CLICK, btnClick2);

                • 5. Re: MP3 Ios External or Internal?
                  Vendita3 Community Member

                  Hi - Thanks for that. Worked straight away.

                  One action for all the sounds. Thanks very much.

                  • 6. Re: MP3 Ios External or Internal?
                    kglad CommunityMVP

                    you're welcome.

                    • 7. Re: MP3 Ios External or Internal?
                      Vendita3 Community Member

                      Hi - I am back trying to perfect this on IOS.

                       

                      When I publish with IOS preview tool, or flash preview the audio is lighting fast. But on an iPhone there is a lag. Just a fraction of a second, but I want to build some apps that are instant.

                       

                      Is there a better way to do with with Flash for IOS. Like the apps out there, drum machines, that sort of thing?

                      • 8. Re: MP3 Ios External or Internal?
                        kglad CommunityMVP

                        you probably will get the most dramatic result using mousedown instead of click.  with touch devices that's what you should be using anyway.

                         

                        you could also eliminate two steps by creating all your sound instances before buttons are pressed:

                         

                        var C:Class;

                        var buttonA:Array = [AString,...];

                        for(var i:int=0;i<buttonA.length;i++){

                        C = Class(getDefinitionByName(buttonA[i].name+"_MP3"));

                        this["sound_"+buttonA[i].name]=new C();

                        }

                         

                        .

                        .

                        .

                        function btnClick2(event:MouseEvent):void {

                        this["sound_"+e.currentTarget.name].play();

                        }

                        AString.addEventListener(MouseEvent.MOUSE_DOWN, btnClick2);

                        • 9. Re: MP3 Ios External or Internal?
                          Vendita3 Community Member

                          Thanks for another update.

                           

                          Here is what I have

                           

                          var C:Class;

                          var e:Class;

                          var buttonA:Array = [AString,BString];

                          for(var i:int=0;i<buttonA.length;i++){

                          C = Class(getDefinitionByName(buttonA[i].name+"_mp3"));

                          this["sound_"+buttonA[i].name]=new C();

                          }

                           

                          function btnClick2(event:MouseEvent):void {

                          this["sound_"+e.currentTarget.name].play();

                          }

                           

                          AString.addEventListener(MouseEvent.MOUSE_DOWN, btnClick2);

                          BString.addEventListener(MouseEvent.MOUSE_DOWN, btnClick2);

                           

                          But unfortunately I get this error:

                           

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

                              at LearnGuitar_fla::MainTimeline/btnClick2()

                           

                          Any ideas on this?

                          • 10. Re: MP3 Ios External or Internal?
                            kglad CommunityMVP

                            C = Class(getDefinitionByName(buttonA[i].name+"_mp3"));

                             

                            should be

                             

                            C = Class(getDefinitionByName(buttonA[i].name+"_MP3"));

                            • 11. Re: MP3 Ios External or Internal?
                              Vendita3 Community Member

                              Thanks for this. I had made it lower case because the actionscript classes against the MP3s I had in lower case.

                              I just tried them with MP3 and changed the code as suggested. Still this error:

                               

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

                                  at FlashMovie_fla::MainTimeline/btnClick2()

                              • 12. Re: MP3 Ios External or Internal?
                                kglad CommunityMVP

                                attach a screenshot of your library showing the class names assigned to some of your sounds like AString_xxx.

                                • 13. Re: MP3 Ios External or Internal?
                                  Vendita3 Community Member

                                  Hi - This should show the library files. Underneath the screen grab I have put the original code modified and working with mouse down. This is already a lot more reactive in IOs, so your new way we are looking at will just be a bonus

                                   

                                  Screen Shot 2013-03-11 at 06.30.06.png

                                   

                                  This works fine and now using MOUSE_DOWN is a lot better in IOS:

                                   

                                  function btnClick2(event:MouseEvent):void {

                                  var C:Class=Class(getDefinitionByName(event.currentTarget.name+"_mp3"));

                                  var c:Sound = new C();

                                  c.play();

                                  }

                                   

                                  PlayAMajor.addEventListener(MouseEvent.MOUSE_DOWN, btnClick2);

                                  PlayBMajor.addEventListener(MouseEvent.MOUSE_DOWN, btnClick2);

                                   

                                  New way that causes the error:

                                   

                                  var C:Class;

                                  var e:Class;

                                  var buttonA:Array = [AString,BString];

                                  for(var i:int=0;i<buttonA.length;i++){

                                  C = Class(getDefinitionByName(buttonA[i].name+"_mp3"));

                                  this["sound_"+buttonA[i].name]=new C();

                                  }

                                   

                                  function btnClick2(event:MouseEvent):void {

                                  this["sound_"+e.currentTarget.name].play();

                                  }

                                   

                                  AString.addEventListener(MouseEvent.MOUSE_DOWN, btnClick2);

                                  BString.addEventListener(MouseEvent.MOUSE_DOWN, btnClick2);

                                  • 14. Re: MP3 Ios External or Internal?
                                    kglad CommunityMVP

                                    change e.currentTarget to event.currentTarget.

                                    • 15. Re: MP3 Ios External or Internal?
                                      Vendita3 Community Member

                                      Thank you very much. This now works (see below). I still need to actually list all the buttons seperately at the end with these lines "AString.addEventListener(MouseEvent.MOUSE_DOWN, btnClick2);"

                                       

                                      Is the purpose of the Array to remove that? Or is the below what you expect to see?

                                       

                                      var C:Class;

                                      var buttonA:Array = [AString,BString];

                                      for(var i:int=0;i<buttonA.length;i++){

                                      C = Class(getDefinitionByName(buttonA[i].name+"_mp3"));

                                      this["sound_"+buttonA[i].name]=new C();

                                      }

                                       

                                      function btnClick2(event:MouseEvent):void {

                                      this["sound_"+event.currentTarget.name].play();

                                      }

                                       

                                      AString.addEventListener(MouseEvent.MOUSE_DOWN, btnClick2);

                                      BString.addEventListener(MouseEvent.MOUSE_DOWN, btnClick2);

                                      • 16. Re: MP3 Ios External or Internal?
                                        kglad CommunityMVP

                                        use:

                                         

                                         

                                         

                                        var C:Class;

                                        var buttonA:Array = [AString,BString];

                                        for(var i:int=0;i<buttonA.length;i++){

                                        buttonA[i].addEventListener(MouseEvent.MOUSE_DOWN,btnClick2);

                                        C = Class(getDefinitionByName(buttonA[i].name+"_mp3"));

                                        this["sound_"+buttonA[i].name]=new C();

                                        }

                                         

                                        function btnClick2(event:MouseEvent):void {

                                        this["sound_"+event.currentTarget.name].play();

                                        }

                                         

                                        • 17. Re: MP3 Ios External or Internal?
                                          Vendita3 Community Member

                                          Excellent. This is spot on.

                                          Many thanks for your help with this. I hope it helps others in the future.

                                          • 18. Re: MP3 Ios External or Internal?
                                            kglad CommunityMVP

                                            you're welcome.