因此,我正在录制音频,然后将剪辑片段保存到页面上并附带波形(使用WaveSurfer.js),但是该波形将以类名波形放置在第一格中。
rec.onstop = e => { console.log(“记录器已停止”);
var trackName = prompt("Enter a name for your sound clip");
var trackContainer = document.createElement("article");
var trackLabel = document.createElement("p");
var audio = document.createElement("audio");
var waveDiv = document.createElement("div");
var deleteButton = document.createElement("button");
trackContainer.classList.add("tracks");
waveDiv.setAttribute("class", "waveform");
audio.setAttribute("class", "clip");
deleteButton.innerHTML = "Delete";
trackLabel.innerHTML = trackName;
trackLabel.setAttribute("class", "track-label");
var btnDiv = document.createElement("div");
var playBtn = document.createElement("button");
var pauseBtn = document.createElement("button");
var play = document.createElement("img");
var pause = document.createElement("img");
btnDiv.setAttribute("class", "btn-div");
playBtn.setAttribute("class", "func-btns");
pauseBtn.setAttribute("class", "func-btns");
play.setAttribute("src", "../assets/play-icon.svg");
play.setAttribute("class", "play-icon");
pause.setAttribute("src", "../assets/pause-icon.svg");
pause.setAttribute("class", "pause-icon");
const playFnc = e => {
myaudio.play();
wavesurfer.play();
};
const pauseFnc = e => {
myaudio.pause();
wavesurfer.pause();
};
playBtn.onclick = playFnc;
pauseBtn.onclick = pauseFnc;
playBtn.appendChild(play);
pauseBtn.appendChild(pause);
btnDiv.appendChild(playBtn);
btnDiv.appendChild(pauseBtn);
trackContainer.appendChild(trackLabel);
trackContainer.appendChild(btnDiv);
trackContainer.appendChild(audio);
trackContainer.appendChild(waveDiv);
trackContainer.appendChild(deleteButton);
soundClips.appendChild(trackContainer);
var blob = new Blob(audioChunks, {
type: "audio/ogg; codecs=opus"
});
audioChunks = [];
var audioURL = window.URL.createObjectURL(blob);
audio.src = audioURL;
var myaudio = new Audio(audioURL);
console.log(audioChunks);
deleteButton.onclick = e => {
var evtTgt = e.target;
evtTgt.parentNode.parentNode.removeChild(evtTgt.parentNode);
};
var wavesurfer = WaveSurfer.create({
container: ".waveform",
waveColor: "#D81E5B",
progressColor: "white",
barGap: "4",
barWidth: "3",
barheight: "1",
height: "100",
scrollParent: false,
cursorWidth: "0",
interact: "true"
});
wavesurfer.load(audioURL);
答案 0 :(得分:0)
我最终将article元素及其内容放入数组中,然后在循环内将child附加到父div,该新div在页面上较旧的音频上方添加了新的音频记录。 Image of the result