[英]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()
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.