javascript - How to use Web Audio API to create .wav file? - Stack Overflow

I am pretty sure I did everything correct but when I try to play or download the file nothing plays. I

I am pretty sure I did everything correct but when I try to play or download the file nothing plays. I am using web audio api to record audio from the microphone to a WAV format. I am using this library to create the .wav file. It seems like nothing is being encoded.

    navigator.mediaDevices.getUserMedia({
        audio: true,video:false
    })
    .then((stream) => {
    var data
    context = new AudioContext()

    var source = context.createMediaStreamSource(stream)
    var scriptNode = context.createScriptProcessor(8192, 1, 1)

    source.connect(scriptNode)
    scriptNode.connect(context.destination)

    encoder = new WavAudioEncoder(16000,1)
    scriptNode.onaudioprocess = function(e){

        data = e.inputBuffer.getChannelData('0')
        console.log(data)
        encoder.encode(data)



    }
    $('#stop').click(()=>{
        source.disconnect()
        scriptNode.disconnect()
        blob = encoder.finish()
        console.log(blob)
        url = window.URL.createObjectURL(blob)
// audio source
        $('#player').attr('src',url)
// audio control
        $("#pw")[0].load()
    })


    })

I am pretty sure I did everything correct but when I try to play or download the file nothing plays. I am using web audio api to record audio from the microphone to a WAV format. I am using this library to create the .wav file. It seems like nothing is being encoded.

    navigator.mediaDevices.getUserMedia({
        audio: true,video:false
    })
    .then((stream) => {
    var data
    context = new AudioContext()

    var source = context.createMediaStreamSource(stream)
    var scriptNode = context.createScriptProcessor(8192, 1, 1)

    source.connect(scriptNode)
    scriptNode.connect(context.destination)

    encoder = new WavAudioEncoder(16000,1)
    scriptNode.onaudioprocess = function(e){

        data = e.inputBuffer.getChannelData('0')
        console.log(data)
        encoder.encode(data)



    }
    $('#stop').click(()=>{
        source.disconnect()
        scriptNode.disconnect()
        blob = encoder.finish()
        console.log(blob)
        url = window.URL.createObjectURL(blob)
// audio source
        $('#player').attr('src',url)
// audio control
        $("#pw")[0].load()
    })


    })
Share Improve this question edited Aug 4, 2018 at 20:42 jeo.e asked Aug 4, 2018 at 20:18 jeo.ejeo.e 2731 gold badge4 silver badges10 bronze badges 1
  • Related: stackoverflow./q/51687308/362536 – Brad Commented Aug 5, 2018 at 6:01
Add a ment  | 

2 Answers 2

Reset to default 4

I figured it out! To help anyone who needs to do the same thing. It uses Web Audio API and this javascript library

navigator.mediaDevices.getUserMedia({
    audio: true,video:false
})
.then((stream) => {


context = new AudioContext()

var source = context.createMediaStreamSource(stream)

var rec = new Recorder(source)
rec.record()




$('#stop').click(()=>{
rec.stop()
blob = rec.exportWAV(somefunction) // exportWAV() returns your file 
})

use recordRTC for recording video and audio, I used in my project, it's working well, here is the code to record audio using recordrtc

 startRecording(event) { // call this to start recording the Audio( or video or Both)
    this.recording = true;
    let mediaConstraints = {
      audio: true
    };

    // Older browsers might not implement mediaDevices at all, so we set an empty object first
    if (navigator.mediaDevices === undefined) {
      navigator.mediaDevices = {};
    }

    // Some browsers partially implement mediaDevices. We can't just assign an object
    // with getUserMedia as it would overwrite existing properties.
    // Here, we will just add the getUserMedia property if it's missing.
    if (navigator.mediaDevices.getUserMedia === undefined) {
      navigator.mediaDevices.getUserMedia = function(constraints) {

        // First get ahold of the legacy getUserMedia, if present
        var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

        // Some browsers just don't implement it - return a rejected promise with an error
        // to keep a consistent interface
        if (!getUserMedia) {
          return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
        }

        // Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
        return new Promise(function(resolve, reject) {
          getUserMedia.call(navigator, constraints, resolve, reject);
        });
      }
    }


    navigator.mediaDevices.getUserMedia(mediaConstraints)
      .then(successCallback.bind(this), errorCallback.bind(this));
  }

 successCallback(stream: MediaStream) {  
    var options = {
          type: 'audio' 
        };
        this.stream = stream;
        this.recordRTC = RecordRTC(stream, options);
        this.recordRTC.startRecording();
  }

errorCallback(stream: MediaStream) {
     console.log(stream);
  }

 stopRecording() { // call this to stop recording 
    this.recording = false;
    this.converting = true;
    let recordRTC = this.recordRTC;
    if(!recordRTC) return;
    recordRTC.stopRecording(this.processAudio.bind(this));
    this.stream.getAudioTracks().forEach(track => track.stop());
  }



processAudio(audioVideoWebMURL) {
    let recordRTC = this.recordRTC;
    var recordedBlob = recordRTC.getBlob(); // you can save the recorded media data in various formats, refer the link below.
    console.log(recordedBlob)
    this.recordRTC.save('audiorecording.wav');
    let base64Data = '';
    this.recordRTC.getDataURL((dataURL) =>  {
      base64Data = dataURL.split('base64,')[1];
      console.log(RecordRTC.getFromDisk('audio', function(dataURL,type) {
        type == 'audio'
      }));
      console.log(dataURL);
     })
}

Note that you cannot record the audio/video from the live site in Google Chrome unless your site is https enabled

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744111847a4558988.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信