开发者

AudioFileWriteBytes returns error -50 randomly

开发者 https://www.devze.com 2023-01-04 08:04 出处:网络
I have the following code snippet: OSStatus status = AudioFileWriteBytes(self.audioFile, FALSE, self.startingByte, &ioNumByt开发者_开发技巧es, theData);

I have the following code snippet:

    OSStatus status = AudioFileWriteBytes(self.audioFile, FALSE, self.startingByte, &ioNumByt开发者_开发技巧es, theData);

The status code randomly returns noErr and -50 on the iPhone simulator.

It then works if I re-run it.

Any pointer is appreciated why the above code behaves randomly.

Thanks in advance for your help.


I think I found my issue.

The original code with the problem:

// Start
OSStatus status = AudioOutputUnitStart(self.ioUnit);

// Record the audio samples and save it to a file
[self createFile];

The new code that fixed the problem. Notice the "createFile" is called first before calling AudioOutputUnitStart

    // Record the audio samples and save it to a file
[self createFile];

// Start
// Once AudioOutputUnitStart is called, it will start calling callback method quickly. We need to call the above [self createFile] first.
OSStatus status = AudioOutputUnitStart(self.ioUnit);

The AudioOutputUnitStart calls the callback method which will write audio samples to a file. Since the file has been created/opened before AudioOutputUnitStart, now the audio samples are written to the file without any error.

0

精彩评论

暂无评论...
验证码 换一张
取 消