AudioFormatWriter iOS

Hello
I run the following code. It works on the simulated device. On the real one, it can’t seem to be able to create the fileOutputStream (null).

		AudioSampleBuffer buffer;
	buffer.setSize(1, 88200);
	for (int i = 0; i < buffer.getNumSamples(); i++)
	{
		buffer.setSample(0, i, .5f);
		if (i < 44100)
		buffer.setSample(0, i, -.5f);
	}
	const File file (File::getSpecialLocation(File::currentApplicationFile).getFullPathName() + "/AudioFiles/NEW.wav");
	DBG("FiLe FULLPATHNAME : " << file.getFullPathName());

	file.deleteFile();
	ScopedPointer<FileOutputStream> fos (file.createOutputStream());
	WavAudioFormat wavFormat;
	AudioFormatWriter* afw =
	wavFormat.createWriterFor(fos, 44100, buffer.getNumChannels(), 16, StringPairArray(), 0);
	afw->writeFromAudioSampleBuffer(buffer, 0, buffer.getNumSamples());

(all this on Xcode 8.3.3, MacMini 2012, OS 10.12.5, targetting iOS 10.3)

Well that’s not surprising - you’re not allowed access to write into the application folder on iOS. (Surprising that the simulator allows it, though)

1 Like

Thanks jules

… but : if I change currentApplicationFile to userDocumentsDirectory, isn’t it supposed to be a little more friendly, with my writing decisions?

Probably. You’d need to look at the path and check the Apple docs to see what the policy is.

And please never concatenate file paths as strings! Keep them as Files and use the provided methods for getting child files, it’ll prevent you falling into other mistakes later!

1 Like

Right, strings do not seem to be very nicely managed. It works better like this:
const File file (File::getSpecialLocation(File::userDocumentsDirectory).getNonexistentChildFile(“New”, “.wav”));
Thanks again.