简体   繁体   中英

Do audio players preload audio files?

I'm creating an Audio Player using Electron and Web Audio API . My current approach for opening and playing audio files looks like this:

  1. User with native dialog window selects audio files - it loads file paths to local storage (application store)

  2. Clicking a "play button" reads the file and converts it to array buffer so the file is acceptable for Web Audio API.

const buffer = toArrayBuffer(fs.readFileSync(filePath));
const audioBuffer = await ctx.decodeAudioData(buffer);
const soundNode = new AudioBufferSourceNode(ctx, { buffer: 
audioBuffer });

Usually, it works fine enough. When I use files over 5MB the opening and conversion shown above tends to take too much time. For example opening 9MB file took ~3seconds . This solution is unacceptable and I have some additional questions.

  • Should I "preload" audio files to local storage when they are being opened in step 1 (open dialog)?
  • Is Electron with Web Audio API not efficient enough to create a desktop player working fast with local files? (I hope not)

Audio files take long to decode because by calling decodeAudioData you ask it to fully decode all the file at once to raw PCM. If someone attempts to open an audio file that is hours long, it will do horrible things to your app, time- and memory-wise. Instead, you may take a look at the MediaElementAudioSourceNode — it can take an <audio> element as a source for Web Audio:

https://developer.mozilla.org/en-US/docs/Web/API/MediaElementAudioSourceNode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM