-1

我有 8 个外部 mp3 已加载并将按顺序播放。我不知道如何暂停当前播放的 mp3 并从暂停点开始播放。我确实有可以暂停它的代码,但是当单击播放按钮时,它会从头开始播放第一个 mp3。

var s1:Sound = new Sound(new URLRequest("Audio Files/1.mp3"));
var s2:Sound = new Sound(new URLRequest("Audio Files/2.mp3"));
var s3:Sound = new Sound(new URLRequest("Audio Files/3.mp3"));
var s4:Sound = new Sound(new URLRequest("Audio Files/4.mp3"));
var s5:Sound = new Sound(new URLRequest("Audio Files/5.mp3"));
var s6:Sound = new Sound(new URLRequest("Audio Files/6.mp3"));
var s7:Sound = new Sound(new URLRequest("Audio Files/7.mp3"));
var s8:Sound = new Sound(new URLRequest("Audio Files/8.mp3"));
s1.addEventListener(Event.COMPLETE, doLoadComplete);
s2.addEventListener(Event.COMPLETE, doLoadComplete);
s3.addEventListener(Event.COMPLETE, doLoadComplete);
s4.addEventListener(Event.COMPLETE, doLoadComplete);
s5.addEventListener(Event.COMPLETE, doLoadComplete);
s6.addEventListener(Event.COMPLETE, doLoadComplete);
s7.addEventListener(Event.COMPLETE, doLoadComplete);
s8.addEventListener(Event.COMPLETE, doLoadComplete);

var channel:SoundChannel = new SoundChannel();

channel = s1.play();
channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete);


function doLoadComplete($evt:Event):void
    {
    trace("Song loaded.");
    }

function doSoundComplete($evt:Event):void
    {
    trace("1 done.");
    channel = s2.play();
    channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete2)
    }

function doSoundComplete2($evt:Event):void
    {
    trace("2 done.");
    channel = s3.play();
    channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete3);
    }`

到目前为止,这是我所拥有的:这会加载 mp3 并播放它们。暂停 btn 有效,但恢复音频的播放按钮给了我一个错误:ReferenceError: Error #1069: Property 0 not found on flash.media.Sound 并且没有默认值。在 mp3sequence_fla::MainTimeline/playSound() 我的猜测是当前位置或最后位置的值不正确。

var myArray:Array=[0,1,2,3,4,5,6,7];
var i:uint=1;
var req:URLRequest = new URLRequest("mp3/"+myArray[i]+".mp3");
var VSound:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0; //last position of the sound
var curSoundIndex:int = 0;  //var to store the current sound that is playing

VSound.load(req);
channel = VSound.play();

function playSound(e:Event = null) {
    //if no sound channel, load the current sound into it
        channel = VSound[curSoundIndex].play(lastPosition);
        channel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete, false, 0, true);
        lastPosition = channel.position;
    }
    function pauseSound(e:Event = null) {
    if (channel) {
        lastPosition = channel.position;
        channel.stop();
    }
}

function doSoundComplete($evt:Event):void {
    curSoundIndex++;
    if (curSoundIndex >= VSound.length) curSoundIndex = 0;
}

play_btn.addEventListener(MouseEvent.CLICK, playSound);
pause_btn.addEventListener(MouseEvent.CLICK, pauseSound);
4

2 回答 2

1

要暂停声音,您可以在暂停时存储它的位置,并使用该变量从该位置重播。

var s:Sound = new Sound(new URLRequest("Audio Files/1.mp3"));
var channel:SoundChannel = new SoundChannel();
channel = s.play();

var pausePosition:int;
function pause():void {
    soundPosition = channel.position;
    channel.stop();
}
function resume():void {
    channel = s.play(soundPosition);
}
于 2014-09-18T23:48:56.297 回答
0

您可以存储position相应的属性SoundChannel。我添加了一些代码来减少整个事情的冗余

var curSoundIndex:int = 0;  //var to store the current sound that is playing
var lastPosition:Number = 0; //last position of the sound
var soundChannel:SoundChannel; 
var sounds:Vector.<Sound> = new Vector.<Sound>(); //array of all sounds

loadNextSound();

function loadNextSound(e:Event = null):void {
    //check if all sounds are loaded
    if (sounds.length >= 8) {
        curSoundIndex = 0; //select first sound
        resume(); //start playing
        return;
    }

    //if not, load the next sound and add it to the array/vector
    var sound:Sound = new Sound(new URLRequest("Audio Files/" + (sounds.length + 1) + ".mp3"));
    sounds.push(sound);
    if(sound.bytesLoaded < sound.bytesTotal){ //check if already loaded
        sound.addEventListener(Event.COMPLETE, loadNextSound);
    }else{
        loadNextSound();
    }

}

function pause(e:Event = null) {
    if (soundChannel) {
        lastPosition = soundChannel.position;
        soundChannel.stop();
    }
}

function resume(e:Event = null) {
    //if no sound channel, load the current sound into it
        soundChannel = sounds[curSoundIndex].play(lastPosition);
        soundChannel.addEventListener(Event.SOUND_COMPLETE, doSoundComplete, false, 0, true); //use weak listener to avoid memory leaks
        lastPosition = 0;
    }
}

function doSoundComplete($evt:Event):void {
    curSoundIndex++;
    if (curSoundIndex >= sounds.length) curSoundIndex = 0;
}
于 2014-09-19T00:00:29.580 回答