[SOLVED] Audio files: zip -> memory, createReaderFor issues


EDIT: I was appending to the MemoryBlock, rather than overwriting the existing (junk) data. I had a suspicion something like this was going on, but the fact that flac somehow still worked(!) led me up the garden path. Now everything is working perfectly. I’ve updated the code below with the simple change and I’ve left the problem here for reference.

I’m trying to load audio into memory from a zip file for playback, and so far I’ve only had success with flac files: in contrast, mp3, ogg and even wav files aren’t working. Perhaps I’m doing something silly, but here goes…

I’m storing the contents of each file in a MemoryBlock then attempting to create a reader from each block. Is this a sane approach or is there something better suited? As an aside, I get the impression that MemoryBlock objects are intended to be used directly rather than via pointers(?), but I can’t seem to get that idiom working at the moment, so for now it’s pointers all the way down.

Anyway, here’s the code:

FileInputStream assetsStream(file);
ZipFile assets(&assetsStream, false);

for (int i = 0; i < assets.getNumEntries(); ++i)
  auto stream = std::unique_ptr<InputStream>(assets.createStreamForEntry(i));

  if (stream != nullptr)
    auto asset = assets.getEntry(i);
    auto assetName = asset->filename;
    auto assetSize = asset->uncompressedSize;
    // auto block = std::make_unique<MemoryBlock>(MemoryBlock(assetSize)); // NO
    // MemoryBlock is appended to, not overwritten
    auto block = std::make_unique<MemoryBlock>(MemoryBlock()); // YES
    stream->readIntoMemoryBlock(*block, assetSize);

where StringArray assetAudioNames; and
std::vector<std::unique_ptr<MemoryBlock>> assetAudio; are the containers.

Then, for any given MemoryBlock* block, the following line produces a nullptr for anything but flac files:

auto reader = (formats.createReaderFor(new MemoryInputStream(*block, false)));

where formats.registerBasicFormats(); has been called earlier.

Stepping through, I can see the reader trying the various formats in turn and ultimately failing. I’ve read that there can be problems finding a reader for certain formats, but in this case it won’t even work for wav(!) and I’ve already been reading these exact same files successfully without any problems when using createReaderFor with BinaryData and regular Files.

Any thoughts? Thanks!