iOS - 播放录制的音频失败,出现 OSStatus 错误 -43(找不到文件)



当我的视图加载时,我通过以下方式设置了一个AVAudioRecorder实例:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
audioSession.delegate = self;
[audioSession setActive:YES error:nil];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
NSString *tempDir = NSTemporaryDirectory();
NSString *soundFilePath = [tempDir stringByAppendingPathComponent:@"sound.m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSLog(@"%@", soundFileURL);
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey,
                                [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey,
                                [NSNumber numberWithInt:16], AVEncoderBitRateKey,
                                [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                                [NSNumber numberWithFloat:8000.0], AVSampleRateKey,
                                [NSNumber numberWithInt:8], AVLinearPCMBitDepthKey,
                                nil];
NSError *error = nil;
self.recorder = [[AVAudioRecorder alloc]
                 initWithURL:soundFileURL
                 settings:recordSettings
                 error:&error];
self.recorder.delegate = self;
if (error) {
    NSLog(@"error: %@", [error localizedDescription]);
} else {
    [self.recorder prepareToRecord];
}

然后,当用户按下录制按钮时,我会这样做:

- (IBAction)record:(id)sender {
    if (!self.isRecording) {
        self.isRecording = YES;
        [self.recorder record];
        self.recordButton.enabled = NO;
    }
}

然后停止录制/播放:

- (IBAction)stop:(id)sender {
    if (self.isRecording) {
        [self.recorder stop];
        self.recordButton.enabled = YES;
        self.isRecording = NO;
    }
    if (self.isPlaying) {
        [self.player stop];
        self.isPlaying = NO;
        self.playButton.enabled = YES;
    }
}

之后,我希望能够播放录制的内容,因此我执行以下操作:

- (IBAction)play:(id)sender {
    NSError *error = nil;
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.recorder.url error:&error];
    self.player.delegate = self;
    if (error) {
        NSLog(@"%@", error.localizedDescription);
    } else {
        self.isPlaying = YES;
        [self.player play];
        self.playButton.enabled = NO;
    }
}

但是当我去播放文件时,找不到OSStatus错误-43文件。

顺便说一句,这一切都在设备上运行,在模拟器上尝试实例化录音机或播放器时出错,从我看到这是一个模拟器问题。

编辑1:我解决了OSStatus错误-43部分,它与编码格式有关,我注释掉了格式,它正确地录制和播放了文件,但我需要以压缩格式录制。有谁知道这个的设置?

编辑2:我设法找到了适用于压缩AAC音频的设置。一个 2 分钟的音频文件达到 256KB,我更新了我的代码以反映当前状态。感谢所有回答您帮助的人。

我要对此

大发雷霆,因为没有其他人回答(编辑:现在有)(我对 iOS 中的音频没有太多经验)但是......

尝试更改

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

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
AVEncoderBitRateKey无法使用

AAC格式编码。所以试试这个代码。

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];
} 

编辑 1:

OSStatus 错误 -43 文件未找到不是模拟器问题。这是一个错误日志,显示您的recording settings are not correct和所需的encoding format is not supported for the settings you have selected

当您收到此错误时,您的文件将以您给定的格式创建。但它会not initialize your audio recorder to start recording.如果您有任何疑问,请选择您在第一个问题和录制中遇到的相同设置,然后尝试播放。由于不支持的设置,它不会录制和播放。

所以对于compressed encoding format,您可以使用我上面在我的代码中给出的设置。您可以为此设置设置.aac格式。它将采用164KB per 10 seconds to record in aac格式。对于其余格式,您必须尝试并弄清楚。

您可能需要

将 AVAudioSession 类别设置回播放模式:

NSError *_error = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&_error];
if (_error) {
    // handle error here
}

这也应该避免在静音开关打开的情况下音频无法播放的问题。

好的,

试试这个:

为您的班级NSURL *soundFileURL全局,以便您可以在类中的任何位置访问它,并以与现在相同的方式进行设置(除了它会soundFileURL = [NSURL fileURLWithPath:soundFilePath];而不是NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];

然后更改此

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

对此

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

相关内容

最新更新