如何删除以前添加的音频元素?

时间:2018-08-27 10:42:44

标签: javascript

其下方的javascript函数用于录制语音并将其显示给用户以下载或收听,但是当用户多次录制将在屏幕上显示许多录制的音频时,我想删除之前的音频用户录制了一个新的,我该如何解决?

      // Success callback
  function(stream) {
     var mediaRecorder = new MediaRecorder(stream);

     visualize(stream);

     record.onclick = function() {
        mediaRecorder.start();
      console.log(mediaRecorder.state);
        console.log("recorder started");
        record.style.background = "red";
        record.style.color = "black";
         record.style.display="none";
         stop.style.display="";
     }

     stop.onclick = function() {
        mediaRecorder.stop();
      console.log(mediaRecorder.state);
        console.log("recorder stopped");
        record.style.background = "";
        record.style.color = "";
           stop.style.display="none";
         record.style.display="block";

     }

     mediaRecorder.ondataavailable = function(e) {
       console.log("data available");

    //   var clipName = prompt('Enter a name for your sound clip');
      var clipName = "record";
       var clipContainer = document.createElement('article');
       var clipLabel = document.createElement('p');
       var audio = document.createElement('audio');
       var deleteButton = document.createElement('button');
       var downloadbutton = document.createElement('a');


       clipContainer.classList.add('clip');
       audio.setAttribute('controls', '');
       deleteButton.innerHTML = "Delete";
       downloadbutton.innerHTML = "download";
       clipLabel.innerHTML = clipName;

       clipContainer.appendChild(audio);
       clipContainer.appendChild(clipLabel);
       clipContainer.appendChild(deleteButton);
        clipContainer.appendChild(downloadbutton);
       soundClips.appendChild(clipContainer);


       var audioURL = window.URL.createObjectURL(e.data);
       audio.src = audioURL;

        downloadbutton.onclick = function(e) {
          //  evtTgt.parentNode.parentNode.removeChild(evtTgt.parentNode);
         downloadbutton.href =  audio.src
          downloadbutton.download = 'record.wav';

       }

       deleteButton.onclick = function(e) {
         evtTgt = e.target;
         evtTgt.parentNode.parentNode.removeChild(evtTgt.parentNode);
       }

     }
  }

0 个答案:

没有答案