AVAudioRecorder在prepareToRecord上抛出异常

时间:2013-11-21 08:07:52

标签: ios objective-c ios7 avaudiorecorder

我在音频录制上运行sample code(可在本文末尾下载源代码)。代码就像这样

NSArray *pathComponents = [NSArray arrayWithObjects:
                           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                           @"MyAudioMemo.m4a",
                           nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

// Initiate and prepare the recorder
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];

然而,在模拟器上运行时,它会在prepareToRecord上抛出异常:

enter image description here

它会记录,如果我只是在异常时关闭断点,它就会正常运行。但这很烦人。怎么了?

1 个答案:

答案 0 :(得分:3)

我也得到了这个例外。寻找解决方案。需要注意的是,如果启用异常断点,则只能看到这些异常。该应用程序运行正常,但这是令人担忧的。我会一直在调查。

编辑: 好吧,答案就在这里:AVAudioPlayer throws breakpoint in debug mode 我自己想的很多,但是对于这些例外情况仍然感到不舒服。录制时使用不同的音频格式会使例外情况消失。但是,我想使用无损格式,这会导致这些异常。希望这有帮助。