开发者

ActionScript play audio

开发者 https://www.devze.com 2023-03-09 09:06 出处:网络
I am just trying to make a simple .swf file that plays a piece of audio when it loads. This compiles but when I bring it up into the browser nothing happens. I could only find sprite based tutorials s

I am just trying to make a simple .swf file that plays a piece of audio when it loads. This compiles but when I bring it up into the browser nothing happens. I could only find sprite based tutorials so I took a stab that you can extend Sound the same way as you would extend Sprite. The final version is going to be headless and called my Java Script to play audio on Events.

 开发者_如何学C  package {
            import flash.media.Sound;
            import flash.net.URLRequest;

            public class typeRight extends Sound {
                    public function HelloWorld( ) {
                    load(new URLRequest('./sound.mp3'));
                    play();
                    }
            }
    }

I am NOT working in Flash so please no GUI advice ; )


Rather than subclassing the Sound class, create a document class like this that contains a Sound class in it:

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;

    public class SoundPlayer extends Sprite
    {
        protected var _sound : Sound;
        protected var _channel : SoundChannel;

        public function SoundPlayer()
        {
            _sound = new Sound();
            _sound.addEventListener(Event.COMPLETE, soundLoadCompleteHandler);
            _sound.addEventListener(IOErrorEvent.IO_ERROR, loadError);
            _sound.load(new URLRequest("./sound.mp3"));
        }

        protected function soundLoadCompleteHandler(evt : Event) : void
        {
            // Use the _channel object to control sound properties such as pan and volume.
            _channel = _sound.play();
        }

        protected function loadError(evt : IOErrorEvent) : void
        {
            trace ("ERROR :: " + evt);
            // You could try recovering from the error here.
        }

    }
}
0

精彩评论

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