Skip navigation
kschwenzer
Currently Being Moderated

AS3 Music Player Help

Apr 24, 2012 2:29 PM

hi,

i'm new to flash and have a final project due tomorrow for my flash class. i'm having issues figuring out the as3 for what i need to do.

 

i have 4 buttons:

-one to increase volume, "volup_btn"

-one to decrease volume, "voldown_btn"

-one to stop music that is playing and change tracks (i have 4 mp3s total the user can click through), "song_btn"

 

the last button is unrelated to the music controls; it changes the background with each click (i have 4 backgrounds the user can click through including the default). i'm also having trouble figuring this actionscript out.

 

i think i have the correct code to control the volume, but i'm having trouble figuring out the code for one button that stops the currently playing music and changes to a different song. i'm guessing i have to create an array, but my teacher didn't go over those and, since i'm pretty new to flash and actionscript, i'm not sure how i would code it to do what i need it to do.

 

this is what i've got so far... i'm sorry if i'm like not even close to the correct code, i'm trying to figure this out as i go along since my teacher has taught us basically no actionscript.

 

var mp3A:Array=["bangarang.mp3", "levels.mp3", "oliphant.mp3", "sweat.mp3"];

var index:int=0;

var resumeTime:Number = 0;

var soundReq:URLRequest = new URLRequest(mp3A[index]);

var mySound:Sound = new Sound();

var channel1:SoundChannel = new SoundChannel();

var volumeAdjust:SoundTransform = new SoundTransform();

 

volumeAdjust.volume = .5;

 

index=(index+1)%mp3A.length;

 

play_btn.addEventListener(MouseEvent.CLICK,changeSong);

volup_btn.addEventListener(MouseEvent.CLICK, volUp);

voldown_btn.addEventListener(MouseEvent.CLICK, volDown);

changelights_btn.addEventListener(MouseEvent.CLICK, changeLights);

 

function changeSong(e:MouseEvent):void {

    if(soundChannel == null){

    soundChannel = sound.play(soundChannelPosition);

    }

    else {

    soundChannelPosition = soundChannel.position;

    soundChannel.stop();

    soundChannel = null;

    }

}

 

function volUp(e:MouseEvent):void{

    volumeAdjust.volume += .1;

    if(volumeAdjust.volume <= 1)

    {

    channel1.soundTransform = volumeAdjust;

    }

    else{

    volumeAdjust.volume = 1;

    channel1.soundTransform = volumeAdjust;

    }

}

 

function volDown(e:MouseEvent):void{

    volumeAdjust.volume -= .1;

    if(volumeAdjust.volume >= 0)

    {channel1.soundTransform = volumeAdjust;

    }

    else

    {

    volumeAdjust.volume = 0;

    channel1.soundTransform = volumeAdjust;

    }

}

 
Replies
  • Currently Being Moderated
    Apr 24, 2012 7:18 PM   in reply to kschwenzer

    What are soundChannel and soundChannelPosition? And sound?

     

    By sound, do you mean mySound? And soundChannel should be channel1 instead? And you didn't define soundChannelPosition either - shouldn't it be declared as a global variable first? (Or maybe you've already done that in the stage, but I can't tell here)

     

    And you really should be loading a song before playing it. Not to mention you did not use the declared array at all.

     

    From the looks of it, you don't seem to fully understand programming in general. Given that and you were not taught to use AS, I think making an MP3 player like this probably isn't what your teacher wanted you to do.

     

    But if you do insist on using the Sound class (instead of making a MovieClip containing all the songs and use simplt movie control script), I recommend you to read this:

     

    http://www.republicofcode.com/tutorials/flash/as3sound/

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points