ActionScript 3.0 按钮控制音乐播放、暂停、停止、循环

ActionScript 3.0 按钮控制音乐播放、暂停、停止、循环

1 按钮

这里用3个Button组成一个MovieClip
MoiveClip的名称为:mcMusic
停止Button的名称为:btnStop
播放Button的名称为:btnPlay
暂停Button的名称为:btnPause

2 播放

从_pos位置开始播放。
注意:play与addEventListener不能互换位置。

private function _play(evt:MouseEvent):void {
    if (_isPause == true) {
        _soundChannel = _music.play(_pos);              
        _soundChannel.addEventListener(Event.SOUND_COMPLETE, _audioComplete);
        _isPause = false;
    }
}

3 暂停

注意:保存播放位置的代码与stop的代码不能互换位置。

private function _pause(evt:MouseEvent):void {
    if (_isPause == false) {
        _pos = _soundChannel.position;
        _soundChannel.stop();
        _isPause = true;
    }
}

4 停止

private function _stop(evt:MouseEvent):void {
    _soundChannel.stop();
    _pos = 0;
    _isPause = true;
}

5 循环

本来Sound的play方法中有一个loops参数用来设置要循环多少次,但是设为0和1的时候都是播放一次,并不能通过这个参数来设置无限循环。这里可以将循环次数设得很大,效果近似于无限循环,不过我还是不喜欢这样做。我的做法是,监听音乐的结束,结束时再播放。

private function _audioComplete(evt:Event): void {
    if (_isPause == false) {
        _soundChannel = _music.play();
        _soundChannel.addEventListener(Event.SOUND_COMPLETE, _audioComplete);
    }
}

6 完整文档类

package  {

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.media.SoundChannel;
    import flash.events.Event;

    public class JMusic extends MovieClip {

        var _music:JCMusic = new JCMusic();
        var _soundChannel:SoundChannel = new SoundChannel();
        var _pos:Number = 0;
        var _isPause = false;

        public function JMusic() {
            mcMusic.btnMusicPlay.addEventListener(MouseEvent.CLICK, _play);
            mcMusic.btnMusicPause.addEventListener(MouseEvent.CLICK, _pause);
            mcMusic.btnMusicStop.addEventListener(MouseEvent.CLICK, _stop);

            _soundChannel = _music.play();
            _soundChannel.addEventListener(Event.SOUND_COMPLETE, _audioComplete);
        }

        private function _play(evt:MouseEvent):void {
            if (_isPause == true) {
                _soundChannel = _music.play(_pos);              
                _soundChannel.addEventListener(Event.SOUND_COMPLETE, _audioComplete);
                _isPause = false;
            }
        }

        private function _pause(evt:MouseEvent):void {
            if (_isPause == false) {
                _pos = _soundChannel.position;
                _soundChannel.stop();
                _isPause = true;
            }
        }

        private function _stop(evt:MouseEvent):void {
            _soundChannel.stop();
            _pos = 0;
            _isPause = true;
        }

        private function _audioComplete(evt:Event): void {
            if (_isPause == false) {
                _soundChannel = _music.play();
                _soundChannel.addEventListener(Event.SOUND_COMPLETE, _audioComplete);
            }
        }
    }
}
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值