-->

iOS版 - 录制的音频回放失败OSStatus错误-43(找不到文件)(iOS - Playbac

2019-06-23 18:31发布

我建立了一个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,我更新了我的代码,以反映当前的状态。 感谢所有谁回答对你有所帮助的。

Answer 1:

我要把野生刺在此,因为没有其他人已经回答(编辑:现在有)(我不具备的iOS与音频多少经验),但...

试着改变

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

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


Answer 2:

AVEncoderBitRateKey不与工作AAC格式encoding.So试试这个代码。

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格式。 对于格式剩下你必须设法弄清楚。



Answer 3:

您可能需要设置AVAudioSession类别回播放模式:

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

if (_error) {
    // handle error here
}

这也应避免在音频不会切换的静音开关发挥的问题。



Answer 4:

好吧试试这个:

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


文章来源: iOS - Playback of recorded audio fails with OSStatus error -43 (file not found)