Flex中的流式声音控制(新手)

时间:2010-08-25 04:04:39

标签: flex actionscript-3

我假设我缺乏知识(我昨天刚刚开始学习Flex,哈!)是我无法弄清楚如何正确工作的原因 - 它甚至可能只是代码定位问题。

我有几个MP3文件,我正在尝试流式传输。现在我只是想开始和停止主MP3。我已成功播放MP3,但停止播放是我遇到的问题。这是我目前的代码:

<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();
    }
]]>

所以我有了song.play工作,但是song.close并没有停止流,它什么也没做。我有什么方法可以正确地做到这一点/我做错了什么?

谢谢! :)

1 个答案:

答案 0 :(得分:3)

您需要使用SoundChannel类来停止播放声音,您可以这样指定:

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();