In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

初めての音テスト

音サンプル
音についてなども含め初心者なので、これから少しずつ勉強していこうと思っていますよ
もっと面白い表現できるようになりたいな
Get Adobe Flash player
by fabure 20 Jul 2009
    Embed
/**
 * Copyright fabure ( http://wonderfl.net/user/fabure )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/flS8
 */

package
{
	import flash.events.SampleDataEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
        import flash.display.Sprite;
	
	/** 音サンプル
	 * 音についてなども含め初心者なので、これから少しずつ勉強していこうと思っていますよ
	 * もっと面白い表現できるようになりたいな
	 */
	public class sampleSound extends Sprite
	{
		
		/** 再生中データ
		 */
		private var soundChannel:SoundChannel;
		
		/**サウンドオブジェクト
		 */
		private var sound:Sound = new Sound();
		
		/** コンストラクタ
		 * @param なし
		 * @return なし
		 */
		public function sampleSound()
		{
			//再生開始
			play();
		}

		/** サンプルデータハンドラー
		 * @param event イベントハンドラー
		 * @return なし
		 */
		private function sampleDataHandler(event:SampleDataEvent):void{
			//サンプルデータの作成
			for(var i:int=0;i<4096;i++){
				var n:Number = Math.sin((i + event.position) / Math.PI / 4);
				
				event.data.writeFloat(n);
				event.data.writeFloat(n);
			}
			
		}
		
		/** 再生停止
		 * @param なし
		 * @return なし
		 */
		public function stop():void{
			this.soundChannel.stop();
		}
		
		/** 再生開始
		 * @param なし
		 * @return なし
		 */
		public function play():void{
			this.sound.addEventListener(SampleDataEvent.SAMPLE_DATA,sampleDataHandler);
			
			//再生開始
			this.soundChannel = this.sound.play();
		}
		
	}
}