SoundEffectクラス [テスト版]
//////////////////////////////////////////////////////////////////////////////
SoundEffectクラス [テスト版]
ここはこうした方がいいんでねぇえの?的なところがあったら、
forkしてくださいまし♪
//////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ipWc
*/
////////////////////////////////////////////////////////////////////////////////
// SoundEffectクラス [テスト版]
//
// ここはこうした方がいいんでねぇえの?的なところがあったら、
// forkしてくださいまし♪
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var se:SoundEffect;
private var source:String = "http://www.project-nya.jp/images/flash/click.mp3";
private var label:Label;
public function Main() {
init();
}
private function init():void {
se = new SoundEffect();
se.addEventListener(Event.COMPLETE, loaded, false, 0, true);
se.load(source, "explosion");
label = new Label(120, 40);
label.x = 232 - 60;
label.y = 232 - 30;
label.text = "♪click";
label.textColor = 0x333333;
}
private function loaded(evt:Event):void {
se.removeEventListener(Event.COMPLETE, loaded);
addChild(label);
stage.addEventListener(MouseEvent.CLICK, click, false, 0, true);
}
private function click(evt:Event):void {
se.play("explosion", 0.2);
}
}
}
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
class SoundEffect extends EventDispatcher {
private static var soundList:Object;
private var sound:Sound;
private var channel:SoundChannel;
private static var initialized:Boolean = false;
private var volume:Number;
private var looping:Boolean = false;
public function SoundEffect() {
if (!initialized) initialize();
}
private static function initialize():void {
initialized = true;
soundList = new Object();
}
public function init(Snd:Class, id:String):void {
var snd:Sound = new Snd();
soundList[id] = snd;
}
public function load(file:String, id:String):void {
var snd:Sound = new Sound();
snd.load(new URLRequest(file));
snd.addEventListener(ProgressEvent.PROGRESS, progress, false, 0, true);
snd.addEventListener(Event.COMPLETE, loaded, false, 0, true);
soundList[id] = snd;
}
public function play(id:String, vol:Number, loop:Boolean = false):void {
if (channel != null) channel.stop();
sound = soundList[id];
volume = vol;
looping = loop;
channel = sound.play();
var transform:SoundTransform = channel.soundTransform;
transform.volume = volume;
channel.soundTransform = transform;
if (looping) {
channel.addEventListener(Event.SOUND_COMPLETE, complete, false, 0, true);
}
}
public function stop():void {
if (channel != null) {
channel.stop();
channel.removeEventListener(Event.SOUND_COMPLETE, complete);
}
}
private function progress(evt:ProgressEvent):void {
dispatchEvent(evt);
}
private function loaded(evt:Event):void {
dispatchEvent(evt);
}
private function complete(evt:Event):void {
channel.removeEventListener(Event.SOUND_COMPLETE, complete);
if (looping) {
channel = sound.play(0);
channel.addEventListener(Event.SOUND_COMPLETE, complete, false, 0, true);
var transform:SoundTransform = channel.soundTransform;
transform.volume = volume;
channel.soundTransform = transform;
}
}
}
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
class Label extends Sprite {
private var txt:TextField;
private static var fontType:String = "Arial";
private var txtWidth:uint;
private var fontSize:uint;
public function Label(w:uint, s:uint) {
txtWidth = w;
fontSize = s;
draw();
}
private function draw():void {
txt = new TextField();
txt.width = txtWidth;
addChild(txt);
txt.autoSize = TextFieldAutoSize.CENTER;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = fontSize;
tf.align = TextFormatAlign.CENTER;
txt.defaultTextFormat = tf;
}
public function set text(param:String):void {
txt.text = param;
}
public function set textColor(param:uint):void {
txt.textColor = param;
}
}