15 Replies Latest reply on Dec 1, 2013 8:26 AM by kglad

    Playing a mp3 when a movie clip loads

    AdobeNewbey

      I am using Adobe Flash Professional CS6

       

      What I am trying to do is play a mp3 file that I have in my library when the clip loads and stop playing it when the a new clip is selected from the menu I created.  On the button that loads the movie clip (Instance name: Slideshow), I have the following code:

      [code]

      on (release){

          gotoAndPlay(120);

      }

      onClipEvent (load){

          mySong = new Sound();

          mySong.attachSound("MastersThisHall");

          mySong.start(0,50);

      }

      onClipEvent (unload) {

          mySong.stop();

      }

      [/code]

      - I went to the properties of the MastersThisHall.mp3 file in my library,

      - Selected the "ActionScript", tab,

      - Checked the box that says "Export for ActionScript",

      - Unchecked the "Export in frame 1" check box, and

      - Added MastersThisHall in the "Identifier" field.

       

      The sound is NOT playing when I when the "Slideshow" button is pressed.

       

      Ideas?

        • 1. Re: Playing a mp3 when a movie clip loads
          kglad Adobe Community Professional & MVP

          attach this code to your main timeline:

           

          mySong = new Sound(this);

             mySong.attachSound("MastersThisHall");

              mySong.start(0,50);

          1 person found this helpful
          • 2. Re: Playing a mp3 when a movie clip loads
            AdobeNewbey Level 1

            When I do that I get a compile error that says "Clip events are permitted only for movie clip instance".


            What I did was this: On frame 120 (the Slideshow instance), I attached this code to the movie clip object.

             

            ------------------------------------------

            onClipEvent (load){

                mySong.attachSound("MastersThisHall");

                mySong.setVolume(vol);

                mySong.start(0,50);

            }

             

            onClipEvent (unload) {

                mySong.stop();

            }

            ------------------------------------------

             

             

            And I put this code in frame 1

            ------------------------------------------

            var vol: Number;

            mySong = new Sound(this);

            ------------------------------------------

            if I do this, I do not get any errors, but it still does not work.

            • 3. Re: Playing a mp3 when a movie clip loads
              kglad Adobe Community Professional & MVP

              follow the suggestion i made in message 1.

              • 4. Re: Playing a mp3 when a movie clip loads
                AdobeNewbey Level 1

                Like I said before, I did that. I placed that code in frame 1.

                 

                This does not work at all. This code implies that is is going to play that file on frame 0 of the flash file.

                 

                Nothing happens.

                • 5. Re: Playing a mp3 when a movie clip loads
                  kglad Adobe Community Professional & MVP

                  the suggestion in message 1 won't trigger that error. you have some other code triggering it.

                   

                  this might be a good time to clean up all your coding by removing all code from movieclips and buttons.

                  1 person found this helpful
                  • 6. Re: Playing a mp3 when a movie clip loads
                    AdobeNewbey Level 1

                    Wierd! It just started playing.

                     

                    I curently only have the following code in my .fla file.

                     

                    ---------------------FRAME 1---------------------

                    SlideshowMusic = new Sound(this);

                    SlideshowMusic.attachSound("MastersThisHall");

                    stop();

                    ------------------------------------------------------------------------

                     

                     

                    ---------------------FRAME 120 (On the Movie Clip Object)---------------------

                    onClipEvent (load) {

                        SlideshowMusic.start(0,1);

                        SlideshowMusic.setVolume(10);

                    }

                     

                    onClipEvent (unload) {

                        SlideshowMusic.stop();

                    }

                    ------------------------------------------------------------------------

                     

                    I am not getting ANY compile errors at all.

                     

                    However, the mySound.setVolume(10); line is not lowering the volume, and the onClipEvent (unload) function is not working (it is supposed to stop playing the file when I select a different movie clip to play.

                     

                    Ideas?

                    • 7. Re: Playing a mp3 when a movie clip loads
                      kglad Adobe Community Professional & MVP

                      on frame 120, if that's where you want to decrease the sound's volume, delete all code attached to your movieclip object) and attach to the main timeline:

                       

                      SlideshowMusic.setVolume(10);

                       

                      if you want to stop the sound, when something (eg, stopSound_mc) is clicked, use:

                       

                      stopSound_mc.onRelease=function(){

                      SlideshowMusic.stop();

                      }

                      1 person found this helpful
                      • 8. Re: Playing a mp3 when a movie clip loads
                        AdobeNewbey Level 1

                        Thank you for all of this; it is very helpful.

                         

                        I have series of 6 buttons I created, each of them load a different movie clip. I need to have the music from the slideshow NOT continue to play when a new movie clip is selected.

                         

                        I have the following code.

                         

                        Frame 1:

                        --------------------------------------------------------------------

                        SlideshowMusic = new Sound(this);

                        SlideshowMusic.attachSound("MastersThisHall");

                        stop();

                        --------------------------------------------------------------------

                         

                        Frame 120 (attached to the Slideshow movie clip object):

                        --------------------------------------------------------------------

                        onClipEvent (load) {

                            SlideshowMusic.play();

                        }

                        --------------------------------------------------------------------

                         

                        The "Mute" Button:

                        --------------------------------------------------------------------

                        on (release) {

                            SlideshowMusic.setVolume(0);

                        }

                        --------------------------------------------------------------------

                         

                        The "Resume Sound" Button:

                        --------------------------------------------------------------------

                        on (release) {

                             SlideshowMusic.setVolume(100);

                        }

                        --------------------------------------------------------------------

                         

                        And the "Pause" Button:

                        --------------------------------------------------------------------

                        on (release)

                        {

                          if (SlideshowMusic.position < SlideshowMusic.duration)

                          {

                            SlideshowMusic.start(SlideshowMusic.position / 1000);

                          }

                        }

                        --------------------------------------------------------------------

                         

                        It is loading and working perfectly. However, I have two more questions:

                         

                        1. How do I make the the sound automatically stop when a new movie clip is played?

                        2. How do I change the "pause" symbol to the "play" symbol when the "Pause" button is pressed?

                        • 9. Re: Playing a mp3 when a movie clip loads
                          kglad Adobe Community Professional & MVP

                          remove all sound from your objects (movieclips and buttons).  assign your buttons and movieclips, that execute code, instance names (in the properties panel).

                           

                          for example, name your "Mute" button (mute_btn), your "Resume Sound" button (unmute_btn), your "Pause" button (pause_btn) and create a play button (play_btn).

                           

                          also, assign your 6 buttons that direct your timeline to different frames instance names (eg, mc0_btn, mc1_btn, mc2_btn,.., mc5_btn) and put those frames in an array:

                           

                          SlideshowMusic = new Sound(this);

                          SlideshowMusic.attachSound("MastersThisHall");

                          stop();

                           

                          play_btn._visible=false;

                          var gotoA:Array=[120,220,100,50,300,72];

                          for(var i:Number=0;i<gotoA.length;i++){

                          this["mc"+i+"_btn"].ivar=i;

                          this["mc"+i+"_btn".onRelease=function(){  //<-this encodes all 6 buttons

                          this.gotoAndStop(gotoA[this.ivar]);

                          pause_btn.onRelease();

                          }

                          }

                           

                          mute_btn.onRelease=function(){

                          SlideshowMusic.setVolume(0);

                          }

                          unmute_btn.onRelease=function{

                          SlideshowMusic.setVolume(100);

                          }

                          pause_btn.onRelease=function(){

                          SlideshowMusic.stop();

                          this._visible=false;

                          play_btn._visible=true;

                          }

                          play_btn.onRelease=function(){

                          if (SlideshowMusic.position < SlideshowMusic.duration)

                            {

                              SlideshowMusic.start(SlideshowMusic.position / 1000);

                            }

                          this._visible=false;

                          pause_btn._visible=true;

                          }

                          • 10. Re: Playing a mp3 when a movie clip loads
                            AdobeNewbey Level 1

                            Wow! That is wonderful. Will this stop the sound when the new movie clip is run?

                            • 11. Re: Playing a mp3 when a movie clip loads
                              kglad Adobe Community Professional & MVP

                              yes, it pauses the sound when any of the 6 buttons is clicked.

                              • 12. Re: Playing a mp3 when a movie clip loads
                                AdobeNewbey Level 1

                                And what happens when the next movie clip has music running in the background? How do I make this work for that as well?

                                • 13. Re: Playing a mp3 when a movie clip loads
                                  kglad Adobe Community Professional & MVP

                                  when you play a movieclip does that mean you goto a frame where that movieclip is located?

                                   

                                  which sounds (ie, what's their linkage id) play when which movieclips (ie, if the answer to the above is yes, which frames correspond to which linkage ids) play?

                                  1 person found this helpful
                                  • 14. Re: Playing a mp3 when a movie clip loads
                                    AdobeNewbey Level 1

                                    Nevermind, it is all working flawlessly now. Thank you very much.

                                    • 15. Re: Playing a mp3 when a movie clip loads
                                      kglad Adobe Community Professional & MVP

                                      you're welcome.