views:

10

answers:

1

I'm assuming my lack of knowledge (I just started learning Flex yesterday, hah!) is the reasoning behind my inability to figure out how to make this work correctly - it may even just be a code positioning issue.

I've got several MP3 files I'm trying to stream. Right now I'm just trying to start and stop the main MP3. I've got the MP3 successfully playing, but stopping it is the issue I'm having. Here's my current code:

<mx:Script>
<![CDATA[
    import flash.events.Event;
    import flash.media.*;
    import flash.media.Sound;
    import flash.media.SoundLoaderContext;
    import flash.net.URLRequest;
    import mx.controls.Button;

    //set current track & load song
            var currentTrack:Number = 1;
    var song:Sound = new Sound();
    var req:URLRequest = new URLRequest("../assets/0"+currentTrack+".mp3");
    var context:SoundLoaderContext = new SoundLoaderContext(8000, true);

    //CREATE BUTTONS (being loaded in mx:application on load)
            private function createControls():void {
        var playButton:Button = new Button();
        playButton.label = "PLAY";
        playButton.id = "playButton";
        playButton.addEventListener(MouseEvent.CLICK, clickPlayHandler);
        playerControls.addChild(playButton);

        var stopButton:Button = new Button();
        stopButton.label = "STOP";
        stopButton.id="stopButton";
        stopButton.addEventListener(MouseEvent.CLICK, clickStopHandler);
        playerControls.addChild(stopButton);
    }

    //HANDLE CLICKS
    private function clickPlayHandler(event:Event):void {
        var button:Button = event.currentTarget as Button;
        song.load(req, context);
        song.play();
    }
    private function clickStopHandler(event:Event):void {
        var button:Button = event.currentTarget as Button;
        //This is not working...
                    song.close();
    }
]]>

So I've got the song.play working, but the song.close doesn't stop the stream, it does nothing. Any clue how I could do this correctly/what I'm doing wrong?

Thanks! :)

+1  A: 

You need to use the SoundChannel class to stop the sound playing, you assign it this way:

private var channel:SoundChannel = new SoundChannel();

//first assign it to the sound variable
channel = sound.play();

//then you can stop your sound this way
channel.stop();
PatrickS
Sweet! Thats exactly what I was missing. Its working perfectly now. Thanks! :)
Ann
You're welcome! Don't forget to mark your question as answered. :)
PatrickS