Button Actionscript 3.0中的音乐播放器

Button Actionscript 3.0中的音乐播放器,button,actionscript,playback,Button,Actionscript,Playback,我已经在autoplay中添加了一个停止按钮,但我需要将其设置为在停止按钮后再次单击该按钮时,音乐开始播放 源代码: var music:Sound = new Sound(new URLRequest("calmingsong.mp3")); var sc:SoundChannel = music.play(); button1.addEventListener(MouseEvent.CLICK, stopMusic); function stopMusic(e:Event):void {

我已经在autoplay中添加了一个停止按钮,但我需要将其设置为在停止按钮后再次单击该按钮时,音乐开始播放

源代码:

var music:Sound = new Sound(new URLRequest("calmingsong.mp3"));
var sc:SoundChannel = music.play();

button1.addEventListener(MouseEvent.CLICK, stopMusic);

function stopMusic(e:Event):void
{
sc.stop();
}

如果您只想从头开始播放声音,只需再次调用
sound
对象的
play()
方法(执行此操作时,您将获得一个新的
SoundChannel
对象)

如果您想在用户停止播放时继续播放声音,则需要添加其他变量来存储当前的“播放状态”。。。大概是这样的:

var music:Sound = new Sound(new URLRequest("calmingsong.mp3"));
var sc:SoundChannel = music.play();
var startPosition:Number = 0;
var isPlaying = true; // default to true cause you auto play...

button1.addEventListener(MouseEvent.CLICK, togglePlayback);

function togglePlayback(e:Event):void
{
    if (isPlaying)
    {
        startPosition = sc.position;
        sc.stop();
        isPlaying = false;
    }
    else
    {
        sc = music.play(startPosition);
        isPlaying = true;
    }
}

非常感谢您,但是我遇到了一个编译器错误-知道如何修复它吗?“1152:与命名空间public中继承的定义flash.display.MovieClip.isPlaying冲突。是的,我选择的变量名称很差,因为MovieClip已经有一个名为“isPlaying”的变量。只需将其更改为其他名称,如“playbackStarted”或其他名称:)