开发者

How to play music tracks in AS3?

I need to play three m开发者_如何学编程usical tracks one after the other in a circle. It is necessary that its are not loaded again and were in the cache. I use this code. Everything works fine on localhost, but only works after restart the app on the server. And wrong to re-download tracks every time.

public function musicOn():void{
    if (sndStart == 'true'){
        req = new URLRequest("media/" + track + ".mp3");
        snd.load(req);
        channel = snd.play(); 
        channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
        sndStart = 'false';
    } else {
        sndStart = 'true';
    }
}

public function musicOff():void{
    if (snd.length>0){
        channel.stop();
        snd = new Sound();
        channel = new SoundChannel();
        sndStart = 'true';
    }   
}

public function onPlaybackComplete(event:Event):void{ 
    if (track==3){
        track = 1;
    } else {
        track++;
    }
    sndStart = 'true';
    snd = new Sound();
    musicOn(); 
}

I run these functions:

if (optObj.music == 'true' && sndStart == 'true'){
    musicOn();
} else if (optObj.music == 'false'){
    musicOff();
}

optObj.music - it is the object with the parameters that is called when the app starts or when I call a function change the settings.


I'm not sure I understand everything you wrote, but how about this one:

package test
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;

public class SoundTest extends MovieClip 
{
    private var files : Array = [ "file1.mp3", "file2.mp3", "file3.mp3" ];
    private var sounds : Array = [];
    private var count : int = 0;
    private var channel : SoundChannel;


    public function musicOff () : void 
    {
        if (channel != null) 
        {
            channel.removeEventListener( Event.SOUND_COMPLETE, onSoundComplete );
            channel.stop( );
            channel = null;
        }
    }

    public function musicOn () : void 
    {
        if (sounds[count] == null) loadSound( );
        else playLoadedSound( );
    }

    private function loadSound () : void 
    {
        var sound : Sound = new Sound( );
        sound.addEventListener( Event.COMPLETE, onSoundLoaded );
        sound.load( new URLRequest( files[count] ) );
    }

    private function playLoadedSound () : void 
    {
        channel = sounds[count].play( );
        channel.addEventListener( Event.SOUND_COMPLETE, onSoundComplete );
    }

    private function onSoundComplete (ev : Event) : void 
    {
        channel = null;
        count++;
        if (count >= files.length) count = 0;
        musicOn( );
    }

    private function onSoundLoaded (ev : Event) : void 
    {
        var snd : Sound = ev.target as Sound;
        sounds.push( snd );
        channel = snd.play( );
        channel.addEventListener( Event.SOUND_COMPLETE, onSoundComplete );
    }

    public function SoundTest ()
    {
        musicOn( );
    }
}
}

(btw You can't play the Sound until it is loaded. That's why your script works only after restart, when the mp3 is in the browser cache.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜