当我的视图加载时,我以以下方式设置了一个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;
    }
}但是当我去播放这个文件时,get OSStatus错误-43文件没有找到。
这都是在设备上运行的,在模拟器上运行是一个错误,当试图实例化记录器或播放机时,我看到这是一个模拟器问题。
编辑1: --我解决了OSStatus错误- -43部分--它与编码格式有关,我注释掉了格式,它正确地记录和播放了文件,但我需要以压缩格式记录。有人知道这个的设置吗?
编辑2: I设法找到了适用于压缩AAC音频的设置。一个2分钟的音频文件出来256 my,我更新了我的代码,以反映当前的状态。感谢所有响应你帮助的人。
发布于 2012-04-23 20:31:21
我会疯狂地尝试这一点,因为没有其他人回答(编辑:现在有)(我对iOS的音频没有太多的经验),但是.
试着改变
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];至
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];发布于 2012-04-29 15:09:49
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格式。对于其余的格式,您必须设法找出。
发布于 2012-04-23 20:33:23
您可能需要将AVAudioSession类别设置为回放模式:
NSError *_error = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&_error];
if (_error) {
    // handle error here
}这也应该避免问题的音频将不会播放的静音开关切换。
https://stackoverflow.com/questions/10218257
复制相似问题