我的声音长度为1:30分钟。我将它嵌入我的swf并将其设置为与帧同步。我需要的是能够通过ActionScript暂停和播放此声音。
有没有人知道如何做到这一点?
答案 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)
我刚做了一个测试,看它是否有效。
以下是基本代码:
//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
中嵌入的各个声音,上面的方法就不行了。在这种情况下,每个Sprite
和MovieClip
类都有一个名为soundTransform
的属性。您可以更改soundTransform
或MovieClip
对象的Sprite
属性并对其进行控制。
您还可以在库中提供与Sound
的链接并动态创建声音。但是,通过这种方式,无法完成同步。