FlashDevelop を使って、サウンドの埋め込みテストをしてみました。
曲は、
フリー素材曲(MIDI/MP3)のダウンロードサイト / ハイパースペシャル さんからお借りしました。
プロジェクトファイルの中に曲データは入っていないので、
HDD の中に入っている MP3 ファイルを持ってきて代用してください。
SWF のファイルサイズが、サウンドが入った分まるごと大きくなるので、
最初にローディング画面を入れた方が良いと思います。
このサンプルでは、ローディング画面は入れていません。。
Flash の実行画面
|
サウンドの埋め込み
|
package
{
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
/**
* サウンドの埋め込みサンプル
* @author Hikipuro
*/
public class Main extends Sprite
{
/**
* ボタン
*/
private var button1:SimpleButton;
/**
* ボタンのラベル
*/
private var buttonText1:TextField;
/**
* 埋め込みサウンド
*/
[Embed(source='../sound/khs_come_cross_the_winter.mp3')]
private static const sound1:Class;
/**
* サウンド
*/
private var sound:Sound;
/**
* サウンド制御用サウンドチャネル
*/
private var soundChannel:SoundChannel;
/**
* コンストラクタ
*/
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
/**
* 初期化イベント
* @param e
*/
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
// ボタンのラベルの作成
buttonText1 = new TextField();
buttonText1.autoSize = TextFieldAutoSize.CENTER;
buttonText1.selectable = false;
buttonText1.x = 160;
buttonText1.y = 110;
buttonText1.text = "再生";
addChild(buttonText1);
// ボタンの作成
button1 = new SimpleButton();
button1.upState = makeRoundRect(0xDDDDDD, 100, 20, 10);
button1.overState = makeRoundRect(0xFFFFFF, 100, 20, 10);
button1.downState = makeRoundRect(0xBBBBBB, 100, 20, 10);
button1.hitTestState = button1.upState;
button1.addEventListener(MouseEvent.MOUSE_DOWN, onButtonMouseDown);
button1.x = 110;
button1.y = 110;
addChild(button1);
// サウンドの初期化
sound = new sound1();
}
/**
* ボタンが押された時のイベント
* @param event
*/
private function onButtonMouseDown(event:MouseEvent):void
{
if (buttonText1.text == "再生")
{
buttonText1.text = "停止";
soundChannel = sound.play(0, 0);
}
else if (buttonText1.text == "停止")
{
buttonText1.text = "再生";
soundChannel.stop();
}
}
/**
* 角丸の図形を描いたスプライトを作って返す
* @param color 色
* @param width 幅
* @param height 高さ
* @param round 角丸の大きさ
* @return スプライト
*/
private function makeRoundRect(color:uint, width:int, height:int, round:int):Sprite
{
var s:Sprite = new Sprite();
s.graphics.lineStyle(2);
s.graphics.beginFill(color);
s.graphics.drawRoundRect(0, 0, width, height, round);
s.graphics.endFill();
s.alpha = 0.3;
return s;
}
}
}