简体   繁体   English

未捕获(承诺)DOMException:无法将音频数据从 pyAudio 解码到 JavaScript

[英]Uncaught (in promise) DOMException: Unable to decode audio data from pyAudio to JavaScript

I have a python app that gets the audio from the microphone, sends it to the server, which in turn sends it to a javascript app.我有一个 python 应用程序,它从麦克风获取音频,将其发送到服务器,然后服务器将其发送到 javascript 应用程序。

I have checked and the data sent by the python app is the same as the one received by the javascript one.我检查过,python 应用程序发送的数据与 javascript 应用程序收到的数据相同。

In the console, in the javascript app, the following message appears: Uncaught (in promise) DOMException: Unable to decode audio data .在控制台中,在 javascript 应用程序中,出现以下消息: Uncaught (in promise) DOMException: Unable to decode audio data

I think the problem is because the data sent is the raw one, without the '.wav' headers, but I also tried writing the data to a file using wave and reading it, the same error appears.我认为问题是因为发送的数据是原始数据,没有“.wav”标头,但我也尝试使用 wave 将数据写入文件并读取它,出现同样的错误。

The data is sent/ received as binary data, using websockets.使用 websockets 以二进制数据的形式发送/接收数据。

Python code: Python代码:

# 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()
  1. The recording data is the one sent to the javascript app.记录数据是发送到 javascript 应用程序的数据。
  2. I know I should record multiple frames, but I've done it this way as it is easier for testing.我知道我应该记录多个帧,但我这样做是因为它更容易测试。

JavaScript code: JavaScript 代码:

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

I've also tried it with the sounddevice python module, but I was getting the same error (the normal method, I couldn't get the Stream callback method to work).我也尝试过使用 sounddevice python 模块,但我遇到了同样的错误(正常方法,我无法让 Stream 回调方法工作)。

Thank you.谢谢你。

I solved it changing the playByteArray to the following function:我解决了将playByteArray更改为以下 function 的问题:

function playByteArray(byteArray) {
    audio = new Audio();
    var blob = new Blob([byteArray], { type: 'audio/wav; codecs=0' });
    var url = window.URL.createObjectURL(blob);     
    audio.src = url;
    audio.oncanplaythrough = (event) => {
        var playedPromise = audio.play();
        if (playedPromise) {
            playedPromise.catch((e) => {
                console.log(e);
                if (e.name === 'NotAllowedError' || e.name === 'NotSupportedError') {
                    console.log(e.name);
                }
            }).then(() => {

            });
        }
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获(承诺)DOMException:无法解码音频数据 - Uncaught (in promise) DOMException: Unable to decode audio data Meteor DOMException:无法解码音频数据 - Meteor DOMException: Unable to decode audio data Web Audio API 不一致无法解码音频数据 DOMException - Web Audio API Inconsistent Unable to decode audio data DOMException 捕获未捕获(承诺)的DOMException javascript - catching Uncaught (in promise) DOMException javascript JavaScript错误:未捕获(已承诺)DOMException - JavaScript Error: Uncaught (in promise) DOMException 如何解决 Web 音频 API 的问题:DOMException:无法解码音频数据 - How to fix issue with Web Audio API: DOMException: Unable to decode audio data 播放 AUDIO 时 Google Chrome Uncaught (in promise) DOMException - Google Chrome Uncaught (in promise) DOMException while playing AUDIO 音频播放器返回 Uncaught (In promise): (DOMException) 该元素没有支持的源 - Audio player returns Uncaught (In promise): (DOMException) The element has no supported sources 浏览器无法处理JS调用的mp3资源,而是将其下载(DOMException:无法解码音频数据) - browser cant process mp3 resources called by JS , instead it will download them (DOMException: Unable to decode audio data) IndexedDB:未捕获(承诺)DOMException - IndexedDB: Uncaught (in promise) DOMException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM