我有一个为IOS9发布的应用程序:一个相对较小的功能是能够录制小的声音片段。在新应用程序上测试类似功能时,我发现相同的代码在运行iOS10的iPad上不起作用,尽管它可以在模拟器上运行。在iPad上,音频计量提供稳定的-120dB,并且永远不会调用audioRecorderDidFinishRecording。没有记录的消息。已发布的应用程序和新应用程序都无法在 iPad 上运行。
代码如下所示:
- (void) record: (int) record_time {
record_sound = self;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err){
NSLog(@"audioSession: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
return;
}
[audioSession setActive:YES error:&err];
err = nil;
if(err){
NSLog(@"audioSession: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
return;
}
NSMutableDictionary * recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
// Create a new file
NSString * recorderFilePath = [Recorder getRecordFilename];
NSFileManager * fileManager = [NSFileManager defaultManager];
NSError * error = nil;
[fileManager createDirectoryAtPath:[recorderFilePath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:&error]; //Create folder
NSURL *url = [NSURL fileURLWithPath:recorderFilePath];
err = nil;
recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];
if(!recorder){
NSLog(@"recorder: %@ %ld %@", [err domain], (long)[err code], [[err userInfo] description]);
return;
}
//prepare to record
[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
// start recording
[recorder recordForDuration:(NSTimeInterval) record_time];
recordEndTime = [[NSDate date] dateByAddingTimeInterval: (NSTimeInterval) (record_time + 0.5)];
}
我关闭了iPad并重新启动了它,一切又开始了。一定有什么东西被锁在AVAudioRecorder机制中。