未捕获(承诺)DOM异常:无法将音频数据从自解码为 JavaScript

我有一个python应用程序,它从麦克风获取音频,将其发送到服务器,服务器又将其发送到javascript应用程序。


我已经检查过,python应用程序发送的数据与javascript应用程序接收的数据相同。


在控制台的 java 脚本应用程序中,将显示以下消息:。Uncaught (in promise) DOMException: Unable to decode audio data


我认为问题是因为发送的数据是原始数据,没有“.wav”标头,但我也尝试使用wave将数据写入文件并读取它,出现相同的错误。


数据作为二进制数据发送/接收,使用网页密码。


蟒蛇代码:


# self data in init

self.sampleRate = 44100

self.duration = 1 / 30

self.channels = 2

self.chunk = 1024

self.format = pyaudio.paInt16


# code

pyAudio = pyaudio.PyAudio()

frames = []

stream = pyAudio.open(

    format=self.format,

    channels=self.channels,

    rate=self.sampleRate,

    input=True,

    output=True,

    frames_per_buffer=self.chunk

)


data = stream.read(int(44100 / self.chunk * self.duration))

frames.append(data)

recording = data


stream.stop_stream()

stream.close()

pyAudio.terminate()

记录数据是发送到 java 脚本应用程序的数据。

我知道我应该录制多个帧,但我这样做了,因为它更容易测试。

脚本代码:


function playByteArray(byteArray) {

    var arrayBuffer = new ArrayBuffer(byteArray.length);

    var bufferView = new Uint8Array(arrayBuffer);

    for (i = 0; i < byteArray.length; i++) {

      bufferView[i] = byteArray[i];

    }

    context.decodeAudioData(arrayBuffer, function(buffer) {

        buf = buffer;

        play();

    });

}


function play() {

    var source = context.createBufferSource();

    source.buffer = buf;

    source.connect(context.destination);

    source.start(0);

}

我也用声音设备python模块尝试过它,但我得到了同样的错误(正常方法,我无法让Stream回调方法工作)。


蓝山帝景
浏览 154回答 1
1回答

慕的地8271018

我解决了将 更改为以下函数的问题:playByteArrayfunction playByteArray(byteArray) {&nbsp; &nbsp; audio = new Audio();&nbsp; &nbsp; var blob = new Blob([byteArray], { type: 'audio/wav; codecs=0' });&nbsp; &nbsp; var url = window.URL.createObjectURL(blob);&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; audio.src = url;&nbsp; &nbsp; audio.oncanplaythrough = (event) => {&nbsp; &nbsp; &nbsp; &nbsp; var playedPromise = audio.play();&nbsp; &nbsp; &nbsp; &nbsp; if (playedPromise) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playedPromise.catch((e) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (e.name === 'NotAllowedError' || e.name === 'NotSupportedError') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(e.name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).then(() => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript