Forum Discussion
Ability for users to record and playback sound during a quiz
This code works very well in the storyline. The issue just that should put a particular time to record in "await sleep(5000);" calculated by milliseconds in the code below. You can just put it directly inside a trigger using (Execute JavaScript) anywhere, then it will play by itself after finishing recording.
There are other ways to save recording but I tried about one year ago and no way could work. I hope it can work in new versions.
You can put it in Command, shape... or even in the player tab.
It could be used for practicing speaking for students.
Hope it can help.
Here's the code:
const recordAudio = () =>
new Promise(async resolve => {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const mediaRecorder = new MediaRecorder(stream);
const audioChunks = [];
mediaRecorder.addEventListener("dataavailable", event => {
audioChunks.push(event.data);
});
const start = () => mediaRecorder.start();
const stop = () =>
new Promise(resolve => {
mediaRecorder.addEventListener("stop", () => {
const audioBlob = new Blob(audioChunks);
const audioUrl = URL.createObjectURL(audioBlob);
const audio = new Audio(audioUrl);
const play = () => audio.play();
resolve({ audioBlob, audioUrl, play });
});
mediaRecorder.stop();
});
resolve({ start, stop });
});
const sleep = time => new Promise(resolve => setTimeout(resolve, time));
(async () => {
const recorder = await recordAudio();
recorder.start();
await sleep(5000);
const audio = await recorder.stop();
audio.play();
})();
Related Content
- 5 months ago