Transcript
Page 1: 20150829 firefox-os-handson
Page 2: 20150829 firefox-os-handson
Page 3: 20150829 firefox-os-handson
Page 4: 20150829 firefox-os-handson
Page 5: 20150829 firefox-os-handson
Page 6: 20150829 firefox-os-handson
Page 7: 20150829 firefox-os-handson
Page 8: 20150829 firefox-os-handson
Page 9: 20150829 firefox-os-handson
Page 10: 20150829 firefox-os-handson
Page 11: 20150829 firefox-os-handson
Page 12: 20150829 firefox-os-handson
Page 13: 20150829 firefox-os-handson
Page 14: 20150829 firefox-os-handson
Page 15: 20150829 firefox-os-handson
Page 16: 20150829 firefox-os-handson
Page 17: 20150829 firefox-os-handson
Page 18: 20150829 firefox-os-handson
Page 19: 20150829 firefox-os-handson
Page 20: 20150829 firefox-os-handson
Page 21: 20150829 firefox-os-handson
Page 22: 20150829 firefox-os-handson
Page 23: 20150829 firefox-os-handson
Page 24: 20150829 firefox-os-handson
Page 25: 20150829 firefox-os-handson
Page 26: 20150829 firefox-os-handson
Page 27: 20150829 firefox-os-handson
Page 28: 20150829 firefox-os-handson
Page 29: 20150829 firefox-os-handson
Page 30: 20150829 firefox-os-handson
Page 31: 20150829 firefox-os-handson
Page 32: 20150829 firefox-os-handson
Page 33: 20150829 firefox-os-handson
Page 34: 20150829 firefox-os-handson
Page 35: 20150829 firefox-os-handson
Page 36: 20150829 firefox-os-handson
Page 37: 20150829 firefox-os-handson
Page 38: 20150829 firefox-os-handson
Page 39: 20150829 firefox-os-handson
Page 40: 20150829 firefox-os-handson
Page 41: 20150829 firefox-os-handson
Page 42: 20150829 firefox-os-handson
Page 43: 20150829 firefox-os-handson

<body> <h1>こんにちは!せかい!</h1></body>

Page 44: 20150829 firefox-os-handson
Page 45: 20150829 firefox-os-handson
Page 46: 20150829 firefox-os-handson
Page 47: 20150829 firefox-os-handson

<head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1">

<title>Hello World</title> <link rel="stylesheet" href="app.css"> <script src="app.js" defer></script></head>

Page 48: 20150829 firefox-os-handson
Page 49: 20150829 firefox-os-handson
Page 50: 20150829 firefox-os-handson
Page 51: 20150829 firefox-os-handson

body{ font-size: 18px; overflow: hidden; background-color: #00539F;}

h1{ font-size: 640%; line-height: .8; color: white;}

Page 52: 20150829 firefox-os-handson
Page 53: 20150829 firefox-os-handson
Page 54: 20150829 firefox-os-handson
Page 55: 20150829 firefox-os-handson

var handleDeviceLightEvent = function(event){ var indicator = document.querySelector("h1"); var lux = event.value;if(indicator != null){indicator.textContent = lux;

}}; window.addEventListener("devicelight", handleDeviceLightEvent);

Page 56: 20150829 firefox-os-handson
Page 57: 20150829 firefox-os-handson
Page 58: 20150829 firefox-os-handson
Page 59: 20150829 firefox-os-handson
Page 60: 20150829 firefox-os-handson

"type": "privileged","permissions": { "device-storage:music": { "access": "readwrite", "description": "録音データの保存ため"

}, "audio-capture": { "description": "録音のため"

}, "desktop-notification": { "description": "保存できたかどうかを通知するため"

}}

Page 61: 20150829 firefox-os-handson

function initializeAudioStream(){ navigator.getUserMedia({video: false, audio:true}, function(_stream){ console.log("ストリーム取得")

stream = _stream; initializeRecorder(); initializeVisualizer(); ui.startButton.disabled = false; }, function(error){ console.log(error); });}

Page 62: 20150829 firefox-os-handson

function initializeRecorder(){ if(stream != null){ console.log("MediaRecorder 初期化")

recorder = new MediaRecorder(stream);

recorder.addEventListener("stop", function(event){ toggleRecordButtonState(); });

recorder.addEventListener("dataavailable", function(event){ console.log("blob取得");

saveCapturedData(event.data); }); }}

Page 63: 20150829 firefox-os-handson

function initializeStorage(){ storage = navigator.getDeviceStorage("music");}

Page 64: 20150829 firefox-os-handson

function saveCapturedData(blob){ if(storage != null){

var filename = createFileName(); var req = storage.addNamed(blob, filename); req.onsuccess = function(){ console.log(this.result + "に保存");

};

req.onerror = function(){ console.log(this.error.name + ":" + this.error.message); }; }}

Page 65: 20150829 firefox-os-handson
Page 66: 20150829 firefox-os-handson

function initializeVisualizer(){ if(stream != null && ui.indicator != null){ update(); }}

Page 67: 20150829 firefox-os-handson

var audioContext= new AudioContext();visualizer.sourceNode = audioContext.createMediaStreamSource(stream);

visualizer.analyser = audioContext.createAnalyser();visualizer.analyser.fftSize = 32;visualizer.buffer = new Uint8Array(visualizer.analyser.frequencyBinCount);visualizer.sourceNode.connect(visualizer.analyser);

visualizer.audioContext = audioContext;

Page 68: 20150829 firefox-os-handson
Page 69: 20150829 firefox-os-handson

visualizer.canvas = ui.indicator;visualizer.graphicsContext = visualizer.canvas.getContext("2d");

Page 70: 20150829 firefox-os-handson

function initializeVisualizer(){ if(stream != null && ui.indicator != null){ var audioContext= new AudioContext(); visualizer.sourceNode = audioContext.createMediaStreamSource(stream);

visualizer.analyser = audioContext.createAnalyser(); visualizer.analyser.fftSize = 32; visualizer.buffer = new Uint8Array(visualizer.analyser.frequencyBinCount);

visualizer.sourceNode.connect(visualizer.analyser);

visualizer.audioContext = audioContext; visualizer.canvas = ui.indicator; visualizer.graphicsContext = visualizer.canvas.getContext("2d");

update(); } }

Page 71: 20150829 firefox-os-handson

get volume(){ this.analyser.getByteFrequencyData(this.buffer); return scale(average(this.buffer), 0, 128);}

Page 72: 20150829 firefox-os-handson

function doUpdateVisualizer(){ var gc = visualizer.graphicsContext; gc.fillStyle = "white"; gc.fillRect(0, 0, visualizer.canvas.width, visualizer.canvas.height); var h = visualizer.volume * visualizer.canvas.height; gc.fillStyle = "green"; gc.fillRect(0, visualizer.canvas.height - h, visualizer.canvas.width, h);}

Page 73: 20150829 firefox-os-handson

req.onsuccess = function(){ console.log(this.result + "に保存");

new Notification("IC Recorder", { body: "音声を " + filename +

" に保存しました。",

icon: "/icons/icon16x16.png" });};

Page 74: 20150829 firefox-os-handson