iOS - Playback of recorded audio fails with OSStatus error -43 (file not found)

泄露秘密 提交于 2019-11-30 15:14:40

I'm gonna take a wild stab at this since no-one else has answered (EDIT: now there is) (I don't have much experience with Audio in iOS) but...

Try changing

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];

to

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

AVEncoderBitRateKey is not working with AAC format encoding.So try this code.

NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSLog(@"%@", soundFileURL);

 NSDictionary *recordSettings = [NSDictionary 
                                        dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithInt:kAudioFormatMPEG4AAC],
                                        AVFormatIDKey,
                                        [NSNumber numberWithInt: AVAudioQualityMax],
                                        AVEncoderAudioQualityKey,
                                        [NSNumber numberWithInt: 1], 
                                        AVNumberOfChannelsKey,
                                        [NSNumber numberWithFloat:44100.0], 
                                        AVSampleRateKey,
                                        nil];
NSError *error = nil;

self.recorder = [[AVAudioRecorder alloc]
                 initWithURL:soundFileURL
                 settings:recordSettings
                 error:&error];
self.recorder.delegate = self;

if (error) {
    NSLog(@"error: %@", [error localizedDescription]);
    NSLog(@"error: %@", [error description]);
} else {
    [self.recorder prepareToRecord];
} 

EDIT 1:

OSStatus error -43 file not found is not a simulator issue. It's an error log, which shows your recording settings are not correct and required encoding format is not supported for the settings you have selected.

When you get this error, you file will get created in the format as you given. But It will not initialize your audio recorder to start recording. If you have any doubt, then select the same settings, which you had in first question and record and try to play that. It won't record and play because of unsupported setting.

So for a compressed encoding format, you can use a settings as I given above in my code. You can have .aac format for this settings. It will take 164KB per 10 seconds to record in aac format. For rest of the formats you have to try and figure out.

You may need to set the AVAudioSession category back to playback mode:

NSError *_error = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&_error];

if (_error) {
    // handle error here
}

This should also avoid the problem where the audio will not play with the mute switch toggled on.

Okay try this:

Make NSURL *soundFileURL global for your class so you can access it anywhere in your class and set in the same way as you are now (except it would be soundFileURL = [NSURL fileURLWithPath:soundFilePath]; instead of NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];.

Then change this

self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.recorder.url error:&error];

to this

self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!