如何使用Web Audio API创建.wav文件? [英] How to use Web Audio API to create .wav file?

查看:157
本文介绍了如何使用Web Audio API创建.wav文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很确定我做的一切都是正确的,但是当我尝试播放或下载文件时,没有任何播放。我正在使用web audio api将麦克风的音频录制为WAV格式。我正在使用此来创建.wav文件。似乎没有任何东西被编码。

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()
    })


    })


推荐答案

我想通了!帮助任何需要做同样事情的人。它使用Web Audio API和这个javascript

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 
})

这篇关于如何使用Web Audio API创建.wav文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆