How do you loop a sound in flash AS3 when it ends?

后端 未结 8 1472
北海茫月
北海茫月 2020-12-16 02:47

What AS3 code is used to loop a sound using AS3?

相关标签:
8条回答
  • 2020-12-16 03:14
    import flash.media.Sound;
    import flash.media.SoundChannel;
    
    var mySound:Sound = new Bgm(); //Bgm() is the class of the internal sound which can be done in the library panel. 
    
    playSound();
    
    function playSound():void
    {
        var channel:SoundChannel = mySound.play();
        channel.addEventListener(Event.SOUND_COMPLETE, onComplete);
    }
    
    function onComplete(event:Event):void
    {
        SoundChannel(event.target).removeEventListener(event.type, onComplete);
        playSound();
    }
    

    This works perfectly.

    0 讨论(0)
  • 2020-12-16 03:14

    this work for me :

    import flash.media.Sound;
    import flash.media.SoundChannel;
    
    var soundUrl:String ="music.mp3";
    var soundChannel:SoundChannel = new SoundChannel();
    var sound:Sound = new Sound();
    
    sound.load(new URLRequest(soundUrl));
    soundChannel = sound.play();
    soundChannel.addEventListener(Event.SOUND_COMPLETE,onComplete);
    
    function onComplete(e:Event):void{
        sound = new Sound();
        sound.load(new URLRequest(soundUrl));
        soundChannel = sound.play();
        soundChannel.addEventListener(Event.SOUND_COMPLETE,onComplete);
    }
    
    0 讨论(0)
  • 2020-12-16 03:16

    This appears to have worked for me:

    var nowTime:Number = (new Date()).time;
    var timeElapsed:Number = nowTime - _lastTime;
    
    _lastTime = nowTime;
    _musicTimeElapsed+=timeElapsed;
    
    if(_musicTimeElapsed >= _musicA.length - GAP_LENGTH)
    {
       _musicTimeElapsed = 0;
       _musicA.play(0);
    }
    
    0 讨论(0)
  • 2020-12-16 03:18

    To expand on @scriptocalypse's gapless playback a bit:

    The problem of not having proper gapless playback comes from mp3 including information about the file in either the head or the tail of the file (id3 tags etc), hence the small pause when you try to loop it. There are a few things you can do depending on your situation.

    1. Ignore it, just play as normal, with a small pause at the end of every file. You can also try and mask it with another sound (a beat drop yo), or fade out and fade in.
    2. If your sounds are embedded, and not streaming, then create a fla file, drag your mp3 in there, and set them to export (the same way you'd add a linkage name for a MovieClip etc). It seems that when you export sounds like this, Flash takes the delay into account, or strips it out when it creates the Sound object. Either way, you can just do a simple play() passing the loops that you want for a gapless playback (I've found using a loops parameter is better than waiting on the SOUND_COMPLETE event and playing it again).
    3. You can try some of the ogg libraries to use .ogg files instead of .mp3. A simple google search for "as3 ogg lib" will turn up what you need. Personally, I found them a bit awkward to use, and I couldn't afford the overhead added (as opposed to mp3 decoding, which is done in the player).
    4. If your mp3 files are streaming, then the only way to get gapless playback is to layer them. Determine the gap (depending on what you used to encode them, it'll be different - my files has a gap of about 330ms), and when you reach it, start playing the overlay. It's a proper pain if you're doing fading, but when it works, it works quite nicely. Worst case scenario, you end up with situation (1)
    0 讨论(0)
  • 2020-12-16 03:18

    I guess this what you looking for in case the voice/music file is in the library:

    var mysound:my_sound = new my_sound();
    mysound.play(0,2); // this will repeat the sound 2 times.
    
    0 讨论(0)
  • 2020-12-16 03:22
    var sound:Sound = whateverSoundYouNeedToPlay;
    function playSound():void
    {
        var channel:SoundChannel = sound.play();
        channel.addEventListener(Event.SOUND_COMPLETE, onComplete);
    }
    
    function onComplete(event:Event):void
    {
        SoundChannel(event.target).removeEventListener(event.type, onComplete);
        playSound();
    }
    
    0 讨论(0)
提交回复
热议问题