flash/カメラインタラクション(動きに音を重ねる)

Last-modified: 2007-02-05 (月) 21:33:40

what

  • Flash側でモーション量を算出してくれるので、それを読み取って表示している。
    適当に閾値を決定して、モーション量が閾値を超えたら音を出すようにしてあります。
    簡易なお遊び監視カメラ程度ならこれの応用で作れてしまうと思う。
    動きの大きさに合わせて音を変えるなんてことも簡単にできるので、適宜いじってください。


ScreenShot

20070123.jpg


Demo

こちらです(カメラ必須・音が鳴ります)
Ver.ライトセイバー


Downloads

hako002.lzh (hako002.fla + VideoCapture.as + hako002.swf + )


Codes

filehako002.fla.txt
//?\ヲ??E?C???h?E?????lash????g??????
Stage.scaleMode = "showAll";
// 100%?\ヲ	--- noScale
// ????\ヲ	--- showAll
// ?O?????????g??--- noBorder
// ???E??t?B?b?g	--- exactFit

//???j???[???}?[?W??
var menu_margin = 100;

//?L???v?`???N???X??C???X?^???X????
var vc:VideoCapture = new VideoCapture(this, myVideo);
init();

// ============================================================
//????UI?????R?[?h
// ============================================================


// ============================================================
//GUI???????C?x???g??o?^
// ============================================================
myComboBox_resolution.addEventListener("change", setResolution);
myComboBox_displaySize.addEventListener("change", setVideoSize);
myStepper_motionLevel.addEventListener("change", setMotionLevel);

//-------------------------------------------------------------
//?T?E???h????
//-------------------------------------------------------------
var mySound0:Sound = new Sound();
var mySound1:Sound = new Sound();
var mySound2:Sound = new Sound();
var mySound3:Sound = new Sound();
var mySound4:Sound = new Sound();
mySound0.attachSound("Sound5");
mySound1.attachSound("Sound1");
mySound2.attachSound("Sound2");
mySound3.attachSound("Sound3");
mySound4.attachSound("Sound4");
vc.setOnActivity( function(isActive:Boolean) {
							var rand = random(5);
							switch( rand ) {
								case 0:
									mySound0.start();
								break;
								case 1:
									mySound1.start();
								break;
								case 2:
									mySound2.start();
								break;
								case 3:
									mySound3.start();
								break;
								case 4:
									mySound4.start();
								break;
								default:
									mySound0.start();
								break;
							}
							var themeColor:String = (isActive) ?  "haloOrange" : "haloGreen";
							myProgressBar_activity.setStyle("themeColor", themeColor); } );
/*
//-------------------------------------------------------------
//activity ??motionLevel ?????????????????
//-------------------------------------------------------------
vc.camera.onActivity = function(isActive:Boolean) {
	mySound.start();
	var themeColor:String = (isActive) ?  "haloOrange" : "haloGreen";
	myProgressBar_activity.setStyle("themeColor", themeColor);
};
*/

//-------------------------------------------------------------
//?J?????x????s??????B
//-------------------------------------------------------------
function init() {
	//?J????R???{?{?b?N?X??I???
	myComboBox_resolution.selectedIndex = 3;	// 3 => 320 x 240
	//????x????
	setResolution();
	setVideoSize();
	setMotionLevel();
}


//-------------------------------------------------------------
//?t???[??????s?????
//-------------------------------------------------------------
onEnterFrame = function() {
	//?p?????[?^??X?V???
	putCameraParameter();
};


//-------------------------------------------------------------
//?p?????[?^??X?V???
//-------------------------------------------------------------
function putCameraParameter() {
	myLabel_fps.text = 'fps : ' + vc.camera.currentFps;
	myLabel_activityLevel.text = vc.camera.activityLevel;
	myProgressBar_activity.setProgress(vc.camera.activityLevel, 100);
}


//-------------------------------------------------------------
//onActivity ????o?決??l??????
//-------------------------------------------------------------
function setMotionLevel() {
	vc.camera.setMotionLevel(myStepper_motionLevel.value, 33);
};


//-------------------------------------------------------------
//????x???X???
//-------------------------------------------------------------
function setResolution() {
	var capture_size_x, capture_size_y;
	switch( myComboBox_resolution.text ) {
		case '40 x 30':
			capture_size_x = 40;
			capture_size_y = 30;
		break;
		case '80 x 60':
			capture_size_x = 80;
			capture_size_y = 60;
		break;
		case '160 x 120':
			capture_size_x = 160;
			capture_size_y = 120;
		break;
		case '320 x 240':
			capture_size_x = 320;
			capture_size_y = 240;
		break;
		case '640 x 480':
			capture_size_x = 640;
			capture_size_y = 480;
		break;
		case '1280 x 960':
			capture_size_x = 1280;
			capture_size_y = 960;
		break;
		default:	//?Y????????????????20 x 240
			capture_size_x = 320;
			capture_size_y = 240;
		break;
	}
	//????x??X??K?p???
	vc.camera.setMode(capture_size_x, capture_size_y, 30);
	//?e?L?X?g???????
	myLabel_resolution_width.text = 'width : ' + vc.camera.width;
	myLabel_resolution_height.text = 'height : ' + vc.camera.height;
}


//-------------------------------------------------------------
//?\ヲ?T?C?Y???X???
//-------------------------------------------------------------
function setVideoSize() {
	var display_size_x, display_size_y;
	switch( myComboBox_displaySize.text ) {
		case 'small':
			display_size_x = 320;
			display_size_y = 240;
		break;
		case 'medium':
			display_size_x = 640;
			display_size_y = 480;
		break;
		case 'large':
			display_size_x = 1280;
			display_size_y = 960;
		break;
		default:	//?Y?????????????????
			display_size_x = Stage.width-menu_margin;
			display_size_y = Stage.height;
		break;
	}
	//??X??K?p???
	vc.setVideoSize(vc.video, (Stage.width-menu_margin)/2 - display_size_x/2, Stage.height/2 - display_size_y/2,
							display_size_x, display_size_y);
}


fileVideoCapture.as.txt

class VideoCapture {
	private var target_mc:MovieClip;	//ゥ?g??^?[?Q?b?g?p?X
	public var video:Video;					//?r?f?I?I?u?W?F?N?g????
	public var camera:Camera;			//????????J??????I?u?W?F?N?g
	
	public function VideoCapture(target:MovieClip, _video:Video) {
		//?n????????i?[
		target_mc = target;
		video = _video;
		//?r?f?I?J?????????@????????_?C?A???O??\ヲ
		System.showSettings(3);
		//?r?f?I??L???v?`???J?
		captureVideo();
	}
	
	private function captureVideo() {
		//?J??????amera????
		camera = Camera.get();
		//?r?f?I?I?u?W?F?N?g??J??????f????\??t???
		video.attachVideo(camera);
		//?r?f?I?I?u?W?F?N?g???u????????
		setVideoSize(video, 0, 0, camera.width, camera.height);	//???????L???v?`?????T?C?Y??\ヲ????
		//Camera ??activity  ??motionLevel ???????A??????????????????
		camera.onActivity = function(isActive:Boolean) {  }
	}
	
	//?r?f?I?I?u?W?F?N?g???u??????????
	public function setVideoSize(arg_video, arg_x, arg_y, arg_width, arg_height) {
		arg_video._x = arg_x;
		arg_video._y = arg_y;
		arg_video._width = arg_width;
		arg_video._height = arg_height;
	}
	
	
	//-------------------------------------------------------------
	//Camera ??activity  ??motionLevel ????????????????????
	//-------------------------------------------------------------
	public function setOnActivity( func ) {
		camera.onActivity = func;
	}
}