Skip to main content
Participating Frequently
November 29, 2013
Answered

Playing a mp3 when a movie clip loads

  • November 29, 2013
  • 1 reply
  • 1413 views

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:

on (release){

    gotoAndPlay(120);

}

onClipEvent (load){

    mySong = new Sound();

    mySong.attachSound("MastersThisHall");

    mySong.start(0,50);

}

onClipEvent (unload) {

    mySong.stop();

}

- 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?

This topic has been closed for replies.
Correct answer kglad

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?


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;

}

1 reply

kglad
Community Expert
Community Expert
November 29, 2013

attach this code to your main timeline:

mySong = new Sound(this);

   mySong.attachSound("MastersThisHall");

    mySong.start(0,50);

Participating Frequently
November 29, 2013

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.

kglad
Community Expert
Community Expert
November 29, 2013

follow the suggestion i made in message 1.