サウンドを使ってみる2


前回のに再生、一時停止、停止、音量UP、DOWNをつけてみました。


左上から右へ、再生、一時停止、停止
下段は音量UP、DOWNです。


こんな感じ


再生

private function start(event:MouseEvent):void{
            //再生中なら関数を抜ける
            if(isPlay) return;
            
            if(channel == null){
                //再生
                channel = snd.play();
                vol = channel.soundTransform.volume;
            }
            else{
                //一時停止したところから再生
                channel = snd.play(pausePos);
            }
            
            //rightBar,leftBarを動かすためのイベントを追加
           addEventListener(Event.ENTER_FRAME, onEnterFrame);
            
           isPlay = true;
}       


一時停止

private function pause(event:MouseEvent):void{
            if(channel != null){
                //一時停止する位置を保存
                pausePos = channel.position;
                //停止
                channel.stop();
                
                //停止している間は必要ないのでイベントの削除
                removeEventListener(Event.ENTER_FRAME, onEnterFrame);
                
                isPlay = false;
            }
}


停止

private function stop(event:MouseEvent):void{
            if(channel != null){
                //停止
                channel.stop();
                channel = null;
                
                removeEventListener(Event.ENTER_FRAME, onEnterFrame);
                
                isPlay = false;
            }
}


音量調整

        private function volume_up(event:MouseEvent):void{
            if(channel == null) return;
            
            //サウンドの音量などを制御するクラス
            var trans:SoundTransform = new SoundTransform();
            vol += 0.1;
            if(vol > 1) vol = 1;
            trans.volume = vol;
            channel.soundTransform = trans;
        }
        
        private function volume_down(event:MouseEvent):void{
            if(channel == null) return;
            
            var trans:SoundTransform = new SoundTransform();
            vol -= 0.1;
            if(vol < 0) vol = 0;
            trans.volume = vol;
            channel.soundTransform = trans;
       }


ソース