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

iOS-播放录制的音频失败,出现OSStatus错误-43(未找到文件),ios,avfoundation,core-audio,avaudioplayer,avaudiorecorder,Ios,Avfoundation,Core Audio,Avaudioplayer,Avaudiorecorder,加载视图时,我按以下方式设置AVAudioRecorder实例: AVAudioSession *audioSession = [AVAudioSession sharedInstance]; audioSession.delegate = self; [audioSession setActive:YES error:nil]; [audioSession setCategory:AVAudioSessionCategoryRecord error:nil]; NSString *tempD

加载视图时,我按以下方式设置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;
    }
}
但是当我去播放时,文件是getOSStatus error-43找不到文件

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

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


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

我想尝试一下,因为没有其他人回答(编辑:现在有了)(我对iOS中的音频没有太多经验),但是

试着改变

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


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

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

if (_error) {
    // handle error here
}
这也可以避免在静音开关打开时音频无法播放的问题。

好的,请尝试以下操作:

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

那就换这个

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

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

avencoderbitractekey
无法使用
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文件不是模拟器问题。它是一个错误日志,显示您的
录制设置不正确
,并且您选择的设置不支持所需的
编码格式

当您遇到此错误时,您的文件将以您给定的格式创建。但它将
不初始化您的录音机以开始录制
。如果您有任何疑问,请选择与第一个问题中相同的设置并录制,然后尝试播放。由于设置不受支持,它将不会录制和播放


因此,对于
压缩编码格式
,您可以使用我在我的代码中上面给出的设置。您可以使用
.aac
格式进行此设置。以aac格式录制将需要
每10秒164KB。对于其余格式,您必须尝试确定。

可能记录器和henc仍在使用该文件e不可用于阅读?(虽然只是在黑暗中摸索)我相信在这种情况下会产生错误,当我点击停止时,录音机停止,所以我对此表示高度怀疑。对不起,如果我在这方面有点错误,因为我几乎没有使用过任何与音频相关的类,但是否有
[self.player prepareToPlay]
?是的,让我编辑我的代码,你正在调用[self.recorder stop]对吗?是的,这可能会阻止我在回答中提到的将类别更改回播放的需要。刚刚注意到,我确实将其设置为在我的代码中播放和录制,但我在我的录制方法中将其设置回录制(dumb me).编辑了我的代码以反映更改。仍然像白噪声一样。修改了它,现在我得到了文件未找到错误。似乎它没有创建音频文件。这节省了我一整天的时间-在比特率和格式上愚弄-结果就是这么简单。非常感谢。为此编辑了我的代码,没有注意到我没有这么做,现在我得到OSStatus错误-43(未找到文件)请小心,您应该测试"setCategory:error:返回YES或NO,不测试error是否为nil。好的,我会尝试一下,但是当我打印出路径时,无论是在创建文件时还是在想回放文件时,路径都是一样的。好的,我明天会仔细看一看。我看到了你的新代码。你的编码格式是kAudioFormatMPEG4AAC,扩展名是.m4a。我想您需要.aac扩展名文件。为此,请使用此代码。这将很好。我看到了错误,但只是在模拟器上。当我停止使用AVEncoderBitRateKey时,它工作得很好。谢谢!
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];
}