root/as2/SoundManager/src/jp/sygnas/sound/SEManager.as

リビジョン 81, 6.2 kB (コミッタ: sygnas, コミット時期: 1 年 前)

--

Line 
1 /*
2 * Copyright 2007 dada
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /*////////////////////////////////////////////////
18 * jp.sygnas.sound.SEManager
19 *
20 * SE管理クラス
21 *
22 * @version              0.99
23 * @author               dada
24 * @url                  http://sygnas.jp/
25 *
26 * BGMと違い、SEは複数同時に呼び出すことがあるので、
27 * SE事にSoundオブジェクトを作成する。
28 * ライブラリからの呼び出しのみ対応。
29 * 全サウンドに対して Soundオブジェクトを作るので、大量登録には注意
30 *
31 * volumeプロパティによる音量変更は、全Soundオブジェクトに対して処理するので注意。
32 * 個別に音量調整するには setVolumeMax( name:String, vol:Number ) を用います
33 *
34 * ////////////////////////////////////////////////
35 * @usage
36 *       var mySE:SEManager = new SEManager(this);
37 *       mySE.addSE( "se1","se1.mp3",50 );
38 *       mySE.play( "bgm1" );
39 *
40 */
41 import mx.utils.Delegate;
42
43 class jp.sygnas.sound.SEManager{
44
45         public var _seList:Object;                              // Soundオブジェクトリスト
46
47         private var $total:Number;                              // 全SE数
48         private var $target:MovieClip;                  // 配置する場所
49         private var $enabled:Boolean;                   // 再生が認められているか
50         private var $gVolume:Number;                    // 全体の音量
51
52         private var $onVolume:Function;                 // ボリュームが変更された
53         private var $onError:Function;                  // 指定された曲がないなどのエラー
54         private var $onEnabled:Function;                // 有効無効切り替え時
55        
56        
57         /* /////////////////////////////////////////
58         * プロパティ
59         */
60        
61         /*
62         * 全体のボリューム
63         * 0~100の範囲で与える。
64         * 曲登録時の基準ボリュームを100として考える。
65         * 50で登録した曲を、50で再生していたら「100」を返す。
66         */
67         public function get volume():Number{ return $gVolume; }
68         public function set volume( vol:Number ){ ___setVolume( vol ); $onVolume( this ); }
69        
70         // 総曲数
71         public function get total():Number{ return $total; }
72
73         // 有効・無効
74         public function get enabled():Boolean{ return $enabled; }
75         public function set enabled( mode:Boolean ){ ___setEnabled( mode ); }
76
77         // イベント
78         public function set onError( func:Function ){ $onError = func; }
79         public function set onEnabled( func:Function ){ $onError = func; }
80
81        
82         /* /////////////////////////////////////////
83         * メソッド
84         */
85        
86         /******************************
87         * コストラクタ
88         * target        soundオブジェクトを割り当てる場所
89         * vol           スタート時のボリューム。指定がなければ「50」
90         */
91         public function SEManager( stage:MovieClip, vol:Number ){
92                 $target = stage;
93                
94                 $total = 0;
95                 $gVolume = vol!=undefined ? vol : 50;
96                 $enabled = true;
97                 _seList = new Object();
98         }
99        
100         /******************************
101         * SEを追加
102         * name                  音声識別子
103         * link                  リンケージ名
104         * vol                   ボリューム   
105         * func                  再生終了時関数
106         */
107         public function addSE( name:String, link:String, vol:Number, func:Function ){
108                 _seList[name] = new Object();
109                 _seList[name]['sound'] = new Sound( $target );
110                 _seList[name]['sound'].attachSound( link )
111                 _seList[name]['sound'].onSoundComplete = func;
112                 _seList[name]['vol'] = vol;
113                 $total ++;
114         }
115        
116         /******************************
117         * 指定されたSEを再生
118         */
119         public function play( name:String ){
120                 if( !$enabled ) return;
121                 if( !___checkExists( name )) return;
122                                                                    
123                 _seList[name]['sound'].start( name );
124         }
125        
126         /******************************
127         * 指定されたSEを停止
128         */
129         public function stop( name:String ){
130                 _seList[name]['sound'].stop();
131         }
132        
133         /******************************
134         * 最大音量を個別に設定
135         * name                  音声識別子
136         * vol                   音量
137         */
138         public function setVolumeMax( name:String, vol:Number ){
139                 _seList[name]['vol'] = Math.max( 0, Math.min( vol, 100 ));
140                 _seList[name]['sound'].setVolume( ___convRealVolume( _seList[name]['vol'] ));
141         }
142        
143         /******************************
144         * 最大音量を個別に取得
145         * name                  音声識別子
146         */
147         public function getVolumeMax( name:String ):Number{
148                 return _seList[name]['vol'];
149         }
150
151         /******************************
152         * SE再生完了時に実行する関数を個別に指定
153         */
154         public function setSoundComplete( name:String, func:Function ){
155                 _seList[name]['sound'].onSoundComplete = func;
156         }
157        
158         /******************************
159         * 破棄する
160         */
161         public function dispose(){
162                 for( var key:String in _seList ){
163                         delete _seList[key]['sound'];
164                         delete _seList[key];
165                 }
166                 delete _seList;
167                 delete this;
168         }
169
170
171         /* /////////////////////////////////////////
172         * プライベートメソッド
173         */
174
175         /******************************
176         * 音声が登録されているか
177         */
178         private function ___checkExists( name ){
179                 if( _seList[name] == undefined ){
180                         $onError( this, "NO_ADDED" );
181                         return false;
182                 }
183                 return true;
184         }
185        
186         /******************************
187         * 全体の音量を設定
188         */
189         private function ___setVolume( vol:Number ){
190                 $gVolume = vol;
191                
192                 var seList:Object = _seList;
193                
194                 for( var key:String in seList ){
195                         seList[key]['sound'].setVolume( ___convRealVolume( seList[key]['vol'] ));
196                 }
197         }
198         /******************************
199         * 実際のボリュームを計算
200         */
201         private function ___convRealVolume( titleVol:Number ):Number{
202                 return titleVol * $gVolume / 100;
203         }
204        
205         /******************************
206         * 有効無効切り替え
207         * 無効なら曲を止める
208         */
209         private function ___setEnabled( mode:Boolean ){
210                 $enabled = mode;
211                 if( !mode ){
212                         var seList:Object = _seList;
213                         for( var key:String in seList ){
214                                 seList[key]['sound'].stop();
215                         }
216                 }
217                 $onEnabled( this, $enabled );
218         }
219        
220        
221 }
222
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。