Creating a .wav file in Android

Hi, i’m trying to create a .wav file with the content of a buffer. What am I doing wrong?

               AndroidDocument destFile = child.createChildDocumentWithTypeAndName("audio/wav", "audiofile.wav");
                DBG("SAVE LOG : created destFile");

                std::unique_ptr<OutputStream> outputStream(destFile.createOutputStream());
                juce::WavAudioFormat wavFormat;
                auto* writer = wavFormat.createWriterFor(outputStream.get(), audioProcessor.getSampleRate(), 2, 16, {}, 0);
                if (writer != nullptr) {
                    //outputStream.release(); // passes responsibility for deleting the stream to the writer object that is now using it
                    int numSamples = audioProcessor.sampleBuffer.getNumSamples();
                    DBG("SAVE LOG : Number of samples: " + String(numSamples));
                    DBG("SAVE LOG : First sample value: " + String(audioProcessor.sampleBuffer.getSample(0, 0)));

                    writer->writeFromAudioSampleBuffer(audioProcessor.sampleBuffer, 0, numSamples);
                    delete writer;
                }

Logcat says:


 W  A resource failed to call AbstractCursor.close. 
2024-09-28 22:02:41.327 11490-11504 System                  com.yourcompany.tesiplugin           W  A resource failed to call CursorWrapperInner.close. 
2024-09-28 22:02:41.327 11490-11504 System                  com.yourcompany.tesiplugin           W  A resource failed to call AbstractCursor.close. 
2024-09-28 22:02:41.327 11490-11504 System                  com.yourcompany.tesiplugin           W  A resource failed to call CursorWrapperInner.close. 
2024-09-28 22:02:41.438 11490-11490 JUCE                    com.yourcompany.tesiplugin           I  JUCE Assertion failure in juce_WavAudioFormat.cpp:1689

Look at the assert in the code:

I am not familiar with the AndroidDocument class, and the docs don’t mention the implementation of OutputStream. But that particular OutputStream seems to not allow to seek back in the stream. The WavFileWriter needs to seek though in order to write the actual file length to the header when closing the writer.