0

我有一个场景,我正在开发一个音频播放器,并想为音轨搜索栏编写代码。

任何帮助表示赞赏。

4

1 回答 1

0

假设您有一个Sound对象和一个SoundChannel对象来播放该Sound对象,您可以执行以下操作:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Rectangle;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;

    public class Main extends Sprite 
    {
        private var _trackBar:Sprite;
        private var _sound:Sound;
        private var _soundChannel:SoundChannel;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            _trackBar = new Sprite();
            _trackBar.graphics.beginFill(0);
            _trackBar.graphics.drawRect(-10, 0, 20, 40);
            _trackBar.graphics.endFill();
            addChild(_trackBar);

            graphics.lineStyle(2);
            graphics.moveTo(_trackBar.x - 10, _trackBar.y);
            graphics.lineTo(_trackBar.x - 10, _trackBar.y +_trackBar.height);
            graphics.moveTo(_trackBar.x + 200, _trackBar.y);
            graphics.lineTo(_trackBar.x + 200, _trackBar.y + _trackBar.height);

            _sound = new Sound();
            _sound.addEventListener(Event.COMPLETE, onLoaded);
            _sound.load(new URLRequest("[mp3 url goes here].mp3"));
        }

        private function onLoaded(e:Event):void 
        {
            _soundChannel = _sound.play();
            stage.addEventListener(Event.ENTER_FRAME, onTick);
            _trackBar.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
            _trackBar.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        }

        private function onTick(e:Event):void 
        {
            if (!_soundChannel)
            {
                return;
            }
            _trackBar.x = 200 * (_soundChannel.position / _sound.length);

        }

        private function onMouseUp(e:MouseEvent):void 
        {
            _trackBar.stopDrag();
            _soundChannel = _sound.play(_sound.length * (_trackBar.x  / 200));

        }

        private function onMouseDown(e:MouseEvent):void 
        {
            _soundChannel.stop();
            _soundChannel = null;
            _trackBar.startDrag(false, new Rectangle(0, 0, 200, 0));
        }

    }

}

请注意:这非常粗糙,绝对可以优化。它只是拼凑在一起,向您展示如何完成这样的任务。

于 2013-03-03T19:29:42.920 回答