Hatena::Groupas3dosue

hidep22の日記

2008-11-13

MP3再生

| 17:09 | はてなブックマーク - MP3再生 - hidep22の日記

AS3(MXML)でMP3を再生しちゃいました!

ちょっと古いですが、Linkin Park & Jay-Zの「Numb Encore」かっこいいですよね。

その中でも印象的なところと言えば、DJがサンプラー叩いているところ。これはなりきるしかない、というわけでサンプラーを作ってみました。

http://www.embodiedweb.net/flex/sampler/index.html

AS(MXML)自体は

<mx:SoundEffect id="dokey" source="do.mp3"/>

で読み込んで、

dokey.sound.play();

で再生するだけです。簡単ですね!

コード書くより、garagebandで音を作る方に時間がかかってしまいました^^

あのフレーズは「ド# ミ ド# ファ# ラ ソ# ド# ミ ド# ラ ソ# ミ」の順番で叩けばOK!

以下、ソース

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init()" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#B6ACAC, #FFFFFF]">
	<mx:Script>
	<![CDATA[		
		private function init():void
		{
			application.addEventListener(KeyboardEvent.KEY_DOWN,keyHandler);
			myCanvas.setFocus();
		}
	
	        public function keyHandler(event:KeyboardEvent): void
	        {
	        	var key:int = event.keyCode;
	        		            
	            if(key==74 || key==100)
	            	dokey.sound.play();
	            if(key==75 || key==101)
	            	mikey.sound.play();
	            if(key==186 || key==107)
	            	fakey.sound.play();
	            if(key==77 || key==97)
	            	sokey.sound.play();
	            if(key==188 || key==98)
	            	rakey.sound.play();
	        }
	]]>
	</mx:Script>
	
	<mx:SoundEffect id="dokey" source="do.mp3"/>
	<mx:SoundEffect id="mikey" source="mi.mp3"/>
	<mx:SoundEffect id="fakey" source="fa.mp3"/>
	<mx:SoundEffect id="sokey" source="so.mp3"/>
	<mx:SoundEffect id="rakey" source="ra.mp3"/>
	
	<mx:Canvas id="myCanvas" width="281" height="224">
		<mx:Button x="28" y="89" label="J(ド#)" width="69"/>
		<mx:Button x="28" y="119" label="M(ソ#)" width="69"/>
		<mx:Button x="83" y="160" label="スタート" width="118" height="42" fontSize="20"/>
		<mx:Label x="56" y="12" text="ジョー・ハーン" fontSize="24" color="#484F51"/>
		<mx:Label x="33" y="46" text="なりきりサンプラー" fontSize="24" color="#484F51"/>
		<mx:Button x="105" y="89" label="K(ミ)" width="69"/>
		<mx:Button x="105" y="119" label=",(ラ)" width="69"/>
		<mx:Button x="182" y="89" label=";(ファ#)" width="77"/>
	</mx:Canvas>
</mx:Application>

rokazrokaz2008/11/16 20:51その気になって叩いててみました!
mxmlについて勉強したいな。

hidep22hidep222008/11/18 17:52MXMLはFlex Builder使いだしてから便利だなーと思いました。
部品を配置していけば勝手にMXMLを書いてくれるのでものすごい楽ですよー。