int16Array blob객체 활용하기
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button class="record">record</button>
<button class="stop">stop</button>
<script>
const record = document.querySelector(".record");
const stop = document.querySelector(".stop");
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
console.log("getUserMedia supported.");
navigator.mediaDevices.getUserMedia(
// constraints - only audio needed for this app
{
audio: true,
}
).then(function (stream) {
//Success CallBack
const mediaRecorder = new MediaRecorder(stream);
record.onclick = function () {
mediaRecorder.start(1000);
console.log(mediaRecorder.state);
console.log("recorder started");
record.style.background = "red";
record.style.color = "black";
let chunks = [];
mediaRecorder.ondataavailable = async function (e) {
// var text = await new Response(e.data).text();
// console.log(text);
// e.data.arrayBuffer().then((resutl) => {
// console.log(resutl);
// });
console.log(e.data.type, 'tpye::');
const fr = new FileReader();
fr.onload = () => {
// It worked
const Data = new Int16Array(fr.result, 0, 2);
console.log(Data);
// ...use the array here...
};
fr.onerror = () => {
// The read failed, handle/report it
};
fr.readAsArrayBuffer(e.data);
// const data = new Int16Array(fr.result);
// console.log(data);
chunks.push(e.data);
// console.log(chunks);
};
};
stop.onclick = function () {
if (mediaRecorder.state = 'recording') {
mediaRecorder.stop();
record.style.background = "";
record.style.color = "";
console.log('stopped recording')
}
}
}).catch(function (err) {
//Error CallBack
console.log(
"The following getUserMedia error occurred: " + err
);
});
} else {
console.log("getUserMedia not supported on your browser!");
}
</script>
</body>
</html>
|
참고한 자료
MediaDevices.getUserMedia()
https://developer.mozilla.org/ko/docs/Web/API/MediaDevices/getUserMedia
https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/stop
https://developer.mozilla.org/en-US/docs/Web/API/FileReader/loadend_event
https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsArrayBuffer
https://developer.mozilla.org/ko/docs/Web/API/MediaDevices
https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_Recording_API
int16Array관련
https://www.javascripture.com/DataView
https://www.javascripture.com/Uint16Array
https://stackoverflow.com/questions/37394541/how-to-convert-blob-to-int16array
https://jeongah-story.tistory.com/179
추가로 좋은 예제 발견
https://codepen.io/robert_bakiev/pen/VpoLYo
다운로드 구현시
https://developer.mozilla.org/en-US/docs/Web/API/MediaStream_Recording_API
'잡단한것들 > 코딩연습장' 카테고리의 다른 글
file.slice is not a function, 비배열 객체, 유사배열 (0) | 2021.08.09 |
---|---|
Node.js, FormData => multer모듈로 업로드 하기 (0) | 2021.07.29 |
Dummy JSON Data (0) | 2021.06.18 |
Color 모듈 만들기 (0) | 2021.05.21 |
lodash를 이용한 scroll 이벤트 (0) | 2021.05.20 |