How can pause / play embed sound in AS3 flash

北慕城南 提交于 2019-12-24 01:58:19

问题


I have a sound with a length of 1:30 min. I embedded it into my swf and set it to sync with the frame. I need is to be able to pause and play this sound via ActionScript.

Does anyone have any idea how to do this?


回答1:


//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;
}



回答2:


I just did a test to see it works.

  1. I made a MovieClip that just has the sound on the timeline( timeline is long enough to hold all the sound)
  2. I've set the Sync to Stream from the Properties Panel on the layer that has the sound. This means that the sound will be in sync with the frames.
  3. I've added two buttons to test controlling the sound, which is just controlling the movie clip that holds the sound sice it's synched with the frames.

Here's the basic code:

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

Hope it helps




回答3:


If you want to control embedded sound globally, There is a class is AS3 called SoundMixer. You can control all sound globally as below code,

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

But if you want to control individual sounds embedded in MovieClips, above method wont work. In this case, Every Sprite and MovieClip class has a property called soundTransform. You can change soundTransform property of objects of MovieClip or Sprite and control them.

You can also give linkage to Sound in library and create sound dynamically. In this way though, syncing couldn't be accomplish.



来源:https://stackoverflow.com/questions/1211393/how-can-pause-play-embed-sound-in-as3-flash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!