I have a broken .mp3 file for testing, I can't catch the error without "app crashed"
The action happens on the backend, then/catch doesn't work with decodeAudioData
const AudioContext = require('web-audio-api').AudioContext;
const context = new AudioContext();
const calcTempo = async (buffer) => {
let audioBuffer;
try {
audioBuffer = await new Promise((resolve, reject) =>
context.decodeAudioData(buffer, resolve, reject)
);
} catch (e) {
console.log(e)
}
}
/usr/src/app/node_modules/av/src/core/stream.js:489 throw new Error("Invalid utf16 sequence."); ^
Error: Invalid utf16 sequence. at Stream.decodeString (/usr/src/app/node_modules/av/src/core/stream.js:489:23) at Stream.readString (/usr/src/app/node_modules/av/src/core/stream.js:403:27) at Class.decodeFrame (/usr/src/app/node_modules/mp3/src/id3.js:117:39) at Class.readFrame (/usr/src/app/node_modules/mp3/src/id3.js:59:30) at Class.read (/usr/src/app/node_modules/mp3/src/id3.js:18:33) at Class.readChunk (/usr/src/app/node_modules/mp3/src/demuxer.js:161:37) at BufferSource. (/usr/src/app/node_modules/av/src/demuxer.js:49:19) at BufferSource.EventEmitter.emit (/usr/src/app/node_modules/av/src/core/events.js:64:12) at BufferSource.loop (/usr/src/app/node_modules/av/src/sources/buffer.js:49:21) at Immediate. (/usr/src/app/node_modules/av/src/sources/buffer.js:4:59) at runCallback (timers.js:693:18) at tryOnImmediate (timers.js:664:5) at processImmediate (timers.js:646:5) at process.topLevelDomainCallback (domain.js:121:23) [nodemon] app crashed - waiting for file changes before starting...
CodePudding user response:
This looks like a mixed approach of Promise
and async-await
.
What if you tried a pure approach such as
const AudioContext = require('web-audio-api').AudioContext;
const context = new AudioContext();
const calcTempo = async (buffer) => {
let audioBuffer;
try {
audioBuffer = await context.decodeAudioData(buffer);
} catch (e) {
console.log(e)
}
}
as suggested here: https://developer.mozilla.org/en-US/docs/Web/API/BaseAudioContext/decodeAudioData
CodePudding user response:
you are missing {} brackets for the callback function
try {
audioBuffer = await new Promise((resolve, reject) =>
{ context.decodeAudioData(buffer, resolve, reject) }
);
}