編集:アプリがバックグラウンドで実行されている場合、メーターCADislayLink
を監視するために使用することはお勧めできません。AVAudioRecorder
デバイスがスリープするとトリガーが停止します(私の場合はデバイスをロックしています)。その解決策は、NSTimer
代わりに使用することです。ここに私の問題を引き起こすコードがあります
- (void)startUpdatingMeter {
// Using `CADisplayLink` here is not a good choice. It stops triggering if lock the device
self.meterUpdateDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleAudioRecorderMeters)];
[self.meterUpdateDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
解決策: 代わりに NSTimer を使用する
// Set timeInterval as frame refresh interval
self.timerMonitor = [NSTimer timerWithTimeInterval:1.f/60.f target:self selector:@selector(handleAudioRecorderMeters:) userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:self.timerMonitor forMode:NSDefaultRunLoopMode];
以下のコードは、またはAVAudioRecorder
を使用していても、完全に動作します。AVAudioSessionCategoryRecord
AVAudioSessionCategoryPlayAndRecord
元の質問: これまでのところ、バックグラウンド モードであってもサウンドを録音するアプリケーションを作成しています。それはかなりiTalkのようなものです。
すべてがほぼ完璧です。アプリはフォアグラウンド/バックグラウンドで記録できます (バックグラウンド モードを登録することにより -リンク) が、デバイスがロックされている場合 (ユーザーまたはセルフデバイスによって) 一時停止/停止します。
iTalk を試してみましたが、その場合は問題なく動作します。iTalk からヒントを得ることもできます。私のアプリにはありませんが、ロック画面に音楽コントロールがあります。
ここに設定する私のコードがAVAudioSession
ありますAVAudioRecorder
- (void)configurateAudioSession {
NSError *error = nil;
// Return success after set category
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error];
// Return success after set active
success = [[AVAudioSession sharedInstance] setActive:YES error:&error];
// Return success after set mode
success = [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoRecording error:&error];
}
- (void)configAudioRecorder {
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath_ = [searchPaths objectAtIndex:0];
NSString *pathToSave = [documentPath_ stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]];
// Create audio recorder
NSURL *url = [NSURL fileURLWithPath:pathToSave];
NSDictionary *settings = @{ AVSampleRateKey: @44100.0,
AVFormatIDKey: @(kAudioFormatAppleLossless),
AVNumberOfChannelsKey: @1,
AVEncoderAudioQualityKey:@(AVAudioQualityMax), };
NSError *error = nil;
self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (error) {
NSLog(@"Error on create audio: %@", error);
}
else {
[self.audioRecorder prepareToRecord];
self.audioRecorder.meteringEnabled = YES;
[self.audioRecorder record];
}
}
どなたか情報をお寄せいただければ大変ありがたく存じます。ありがとう!