A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: External Sound Play Button

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    9

    External Sound Play Button

    This is code loads the music internally:

    var url:String = "C:/Users/George/Desktop/erevemusic.mp3";
    var urlRequest:URLRequest = new URLRequest(url);
    var sound:Sound = new Sound();
    sound.load(urlRequest);
    sound.play();

    And this makes it so when you click the button it stops the music:

    ToggleButton.addEventListener(MouseEvent.CLICK, beQuiet);
    function beQuiet(e:MouseEvent):void
    {
    SoundMixer.stopAll();
    }

    What code do I need to click on ToggleButton button for the music to play again?

  2. #2
    Senior Member guardiankitty's Avatar
    Join Date
    Dec 2006
    Location
    Here
    Posts
    215
    I am not going to test this example, let me know if it works and if there are any errors;


    Actionscript Code:
    var url:String = "C:/Users/George/Desktop/erevemusic.mp3";
    var urlRequest:URLRequest = new URLRequest(url);
    var sound:Sound = new Sound();
    sound.load(urlRequest);
    sound.play();
    var isPlaying:Boolean = true;

    //And this makes it so when you click the button it stops the music:

    ToggleButton.addEventListener(MouseEvent.CLICK, toggleSound);

    function toggleSound(e:MouseEvent):void
    {
    if(isPlaying) //If the sound is playing
        SoundMixer.stopAll();//stop all sounds
    else//otherwise
        sound.play();//play the sound
    isPlaying=!isPlaying;//whatever it was last set to, you are now the opposite.
    }


    ps. there might be a isPlaying variable on the sound object, I cant remember right off the top of my head. You can use that in place of keeping track of the state yourself (if it exists that is).


    Let me know if this works out for you, or if it doesn't compile. Good Luck
    ~!GK!>'.'<
    Last edited by guardiankitty; 01-04-2012 at 08:48 PM.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    9
    Thank you sooooooo much guardiankitty! It works perfectly! I've been trying to figure this about for a good amount of days.

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    9
    Actually now that it works I do have 1 more question:

    Is there any way to make the music loop? (if it's not already)

  5. #5
    Flash Genie letschillout's Avatar
    Join Date
    Feb 2007
    Location
    31.52949, 74.347272
    Posts
    146
    Actionscript Code:
    public function playMusic():void
    {
        musicChannel = music.play();
        musicChannel.addEventListener(Event.SOUND_COMPLETE, loopMusic);
    }

    public function loopMusic(e:Event):void
    {
        if (musicChannel != null)
        {
            musicChannel.removeEventListener(Event.SOUND_COMPLETE, loopBackgroundMusic);
        playMusic();
        }
    }

    http://doogog.com/music-looping-in-as3.html

    or veiw this http://board.flashkit.com/board/showthread.php?t=769120

    Actionscript Code:
    import flash.media.Sound;
    import flash.media.SoundChannel;

    status.text = "Chimes Off";

    playButton.addEventListener(MouseEvent.MOUSE_DOWN, playChimes);

    var chimesSound:SoundChannel;

    function playChimes(e:MouseEvent):void{
        if(status.text == "Chimes Off"){
            var chimes:Chimes = new Chimes();
            chimesSound = chimes.play(0,int.MAX_VALUE);
            status.text = "Chimes On";
        }else{
            chimesSound.stop();  
            status.text = "Chimes Off";
        }
    }
    Charag - 3D, Flash Games, Animations,
    Website Development & More...


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center