I am very new to programming in Vue.js and what I want to do is a simple audio recorder that starts recording audio on click of a recording button and that stops recording on click of a stop button. When stopping it should display the audio file in the template and store the audio in a blob which should be later on stored locally.
I have implemented the template as follows:
<template>
<!-- Voice Record Title + Button -->
<div class="form-group row">
<label for="Audio" class="col-2 col-form-label labelTop">Audio</label>
<div class="col-1">
<button @click="recordAudio()" type="button" id="button_record" class="btn btn-danger">
</button>
<button type="button" id="button_stop" class="btn btn-success">
</button>
<div id="audio" class="audio" controls>
</div>
</div>
</div>
</template>
The script contains the following code:
export default {
methods: {
recordAudio() {
var device = navigator.mediaDevices.getUserMedia({ audio: true });
var items = [];
device.then((stream) => {
var recorder = new MediaRecorder(stream);
recorder.ondataavailable = (e) => {
items.push(e.data);
if (recorder.state == "inactive") {
var blob = new Blob(items, { type: "audio/*" });
var audio = document.getElementById("audio");
var mainaudio = document.createElement("audio");
mainaudio.setAttribute("controls", "controls");
audio.appendChild(mainaudio);
mainaudio.innerHTML =
'<source src="' +
URL.createObjectURL(blob) +
'" type="audio/*" />';
}
};
recorder.start();
// I do not want a timeout to stop the recording but clicking the stop button to do so
setTimeout(() => {
recorder.stop();
}, 5000);
});
},
},
};
This might sound simple, but the only thing that I want to do now is that it stops recording not using this timeout function after some seconds, but to stop recording on click of button_stop.
I tried so many things, e.g. directly setting an event-handler to button_stop with
@click="recorder.stop()"
or also:
if (document.getElementById("button_stop").clicked == true) {
recorder.stop();
}
and also:
document.getElementById("button_stop").addEventListener("click", recorder.stop());
if (document.getElementById("button_stop").clicked == true) {
recorder.stop();
}
but nothing of that really works.
As I am very new to Vue and Javascript generally, this is really frustrating me. I would really would appreciate any help.