如何在AS3闪存中暂停/播放嵌入声音

时间:2009-07-31 08:54:16

标签: flash actionscript-3 audio

我的声音长度为1:30分钟。我将它嵌入我的swf并将其设置为与帧同步。我需要的是能够通过ActionScript暂停和播放此声音。

有没有人知道如何做到这一点?

3 个答案:

答案 0 :(得分:5)

//number that is redefined when the pause button is hit
var pausePoint:Number = 0.00;

//a true or false value that is used to check whether the sound is currently playing
var isPlaying:Boolean;

//think of the soundchannel as a speaker system and the sound as an mp3 player
var soundChannel:SoundChannel = new SoundChannel();
var sound:Sound = new Sound(new URLRequest("SOUND.mp3"));

//you should set the xstop and xplay values to match the instance names of your stop button and play/pause buttons
xstop.addEventListener(MouseEvent.CLICK, clickStop);
xplay.addEventListener(MouseEvent.CLICK, clickPlayPause);

soundChannel = sound.play();
isPlaying = true;

function clickPlayPause(evt:MouseEvent) {
    if (isPlaying) {
        pausePoint = soundChannel.position;
        soundChannel.stop();
        isPlaying = false;
    } else {
        soundChannel = sound.play(pausePoint);
        isPlaying = true;
    }
}

function clickStop(evt:MouseEvent) {
    if (isPlaying) {
        soundChannel.stop();
        isPlaying = false;
    }
    pausePoint = 0.00;
}

答案 1 :(得分:3)

我刚做了一个测试,看它是否有效。

  1. 我制作了一个只在时间轴上有声音的MovieClip(时间轴 足以容纳所有人 声音)
  2. 我已从图层的“属性”面板中设置“同步到流” 有声音。这意味着 声音会与声音同步 帧。
  3. 我添加了两个按钮来测试控制声音,这就是 控制影片剪辑 握住它同步的声音 用框架。
  4. 以下是基本代码:

    //playBtn and pauseBtn are two basic buttons
    //sound is the movie clip that holds the synched sound in its timeline
    playBtn.addEventListener(MouseEvent.CLICK, playSound);
    pauseBtn.addEventListener(MouseEvent.CLICK, pauseSound);
    
    function playSound(event:MouseEvent):void{
        sound.play();
    }
    function pauseSound(event:MouseEvent):void{
        sound.stop();
    }
    

    希望有所帮助

答案 2 :(得分:0)

如果要全局控制嵌入式声音,则有一个类称为SoundMixer的AS3。您可以全局控制所有声音,如下面的代码,

SoundMixer.soundTransform = new SoundTransform(0); //This will mute all sound from SWF.
SoundMixer.soundTransform = new SoundTransform(1); //This will unmute all sound from SWF.

但是如果你想控制MovieClip中嵌入的各个声音,上面的方法就不行了。在这种情况下,每个SpriteMovieClip类都有一个名为soundTransform的属性。您可以更改soundTransformMovieClip对象的Sprite属性并对其进行控制。

您还可以在库中提供与Sound的链接并动态创建声音。但是,通过这种方式,无法完成同步。