iOS - 播放录制的音频失败,OSStatus错误-43(找不到文件) [英] iOS - Playback of recorded audio fails with OSStatus error -43 (file not found)
问题描述
我加载视图时,按以下方式设置AVAudioRecorder实例:
I set up an AVAudioRecorder instance the following way when my view loads:
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];
}
然后,当用户按下记录按钮时,我这样做:
Then, when the user presses the record button I do:
- (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;
}
}
之后,我想能够播放的是什么录音,所以我做了以下几点:
After, I want to be able to playback what was recorded, so I do the following:
- (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 找不到文件。
But when I go and play the file is get OSStatus error -43 file not found.
这一切都在设备上运行btw,在模拟器上尝试实例化记录器时出错或玩家,从我看到它是一个模拟器问题。
This is all running on the device btw, on the simulator y an error when trying to instantiate the recorder or player, and from I've seen it's a simulator issue.
编辑1:我解决了OSStatus错误-43部分它有一些东西为了处理编码格式,我注释掉了格式并正确记录和播放了文件,但我需要以压缩格式录制。有没有人知道这个设置?
EDIT 1: I solved the OSStatus error -43 part it has something to do with the encoding format, I commented out the format and it recorded and played the file properly, but I need to record in a compressed format. Does anyone know the settings for this?
编辑2:我设法找到适用于压缩AAC音频的设置。一个2分钟的音频文件出现了256KB,我更新了我的代码以反映当前的状态。感谢所有为您提供帮助的人。
EDIT 2: I managed to find settings that worked for compressed AAC audio. A 2min audio file comes out to 256KB, I updated my code to reflect the current state. Thanks to all of those who answered for your help.
推荐答案
因为没有人,所以我会采取疯狂的做法别人已经回答了(编辑:现在有)(我对iOS中的音频没有多少经验)但是......
I'm gonna take a wild stab at this since no-one else has answered ( now there is) (I don't have much experience with Audio in iOS) but...
尝试更改
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
到
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
这篇关于iOS - 播放录制的音频失败,OSStatus错误-43(找不到文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!