声音的加载,播放,暂停

package com{
    import flash.display.Sprite;
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.display.MovieClip;
    import flash.media.SoundChannel;
    import flash.display.SimpleButton;
    import flash.events.MouseEvent;
    import flash.display.Stage;
    import flash.events.Event;

    public class MyMusic extends Sprite {

        public static var urlRe:URLRequest;
        public static var urlSt:String;

        public static var sound:Sound;
        public static var soundChannel:SoundChannel=new SoundChannel ();
        public static var soundPosition:Number=0;
        public static var isSoundPlay:Boolean=false;
        public static var _stage:Stage;
        public static var boo:Boolean=true;

        public function MyMusic( ___stage:Stage) {
            //super();
            _stage=___stage;
            //sound=new Sound ();
            //soundChannel=new SoundChannel ();
        }

        //===========初始声音也就是背景音乐===================================
        public function initialSound(_urlSt:String ) {
            if (boo==true) {
                var str=_stage.loaderInfo.url;
                var str1:int=str.lastIndexOf("/");
                var str2:String=str.slice(0,str1+1);
                var str3:String=str.substring(str1+1);
                //trace(str1)
                //trace(str2)
                //trace(str3)
                soundChannel.stop();
                sound=new Sound ();
                urlRe=new URLRequest (str2+"music/"+_urlSt+".mp3");
                soundChannel=new SoundChannel ();
                sound.load(urlRe);
                soundChannel=sound.play();
                soundChannel.addEventListener(Event.SOUND_COMPLETE,soundCompete);
                boo=false;
            }
        }
        //==========切换声音===================================
        public static function changeSound(_urlSt:String ) {
            var str=_stage.loaderInfo.url;
            var str1:int=str.lastIndexOf("/");
            var str2:String=str.slice(0,str1+1);
            var str3:String=str.substring(str1+1);
            //trace(str1)
            //trace(str2)
            //trace(str3)
            soundChannel.stop();
            sound=new Sound ();
            urlRe=new URLRequest (str2+"music/"+_urlSt+".mp3");
            soundChannel=new SoundChannel ();
            sound.load(urlRe);
            soundChannel=sound.play();
        }
        //===========在暂停的地方继续播放===============================================================================
        //暂停
        public static function pauseFuntion() {
            soundPosition=soundChannel.position;
            soundChannel.stop();
            isSoundPlay=true;
        }
        //播放;
        public static function playFuntion() {
            if (isSoundPlay) {
                soundChannel=sound.play(soundPosition);
                soundChannel.addEventListener(Event.SOUND_COMPLETE,soundCompete);
                isSoundPlay=false;
            }
        }
        public static function soundCompete(e:Event) {
            soundChannel=sound.play();
            soundChannel.addEventListener(Event.SOUND_COMPLETE,soundCompete);
        }
    }
}

 

posted on 2018-06-12 10:21  zqiang0803  阅读(166)  评论(0编辑  收藏  举报

导航