我正在尝试实现一些我认为非常容易的事情,但事实证明并非如此。
我正在使用“react-native-audio”库的源代码。但是你可以假设我为了这个问题而在本地工作。
这里是 reference for the source code我在玩。
我的目标很简单,我正在使用 AVAudioRecorder 录制 session (大约需要 30 分钟)。如果在录音过程中有来电,我希望我的应用能够通过执行以下选项之一“恢复”:
1) 当应用回到前台时,“暂停”“来电”和“恢复”记录。
2) 来电时 - 关闭当前文件,当应用程序返回前台时,开始使用新文件进行新录制(第 2 部分)。
显然选项 (1) 是首选。
请注意,我很清楚使用 AVAudioSessionInterruptionNotification 并在我的实验中使用它到目前为止没有运气,例如:
- (void) receiveAudioSessionNotificationNSNotification *) notification
{
if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
NSLog(@"AVAudioSessionInterruptionNotification");
NSNumber *type = [notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey];
if ([type isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
NSLog(@"*** InterruptionTypeBegan");
[self pauseRecording];
} else {
NSLog(@"*** InterruptionTypeEnded");
[_recordSession setActive:YES error:nil];
}
}
}
请注意,我将为这个问题设置赏金,但唯一可接受的答案将是真实世界的工作代码,而不是“理论上应该工作”的东西。非常感谢您的帮助
Best Answer-推荐答案 strong>
我选择AVAudioEngine 和AVAudioFile 作为解决方案,因为代码很简短,AVFoundation的中断处理是particularly simple (您的播放器/录音机对象已暂停,取消暂停会重新激活您的 Audio Session )。
N.B AVAudioFile 没有明确的关闭方法,而是在 dealloc 期间写入标题并关闭文件,令人遗憾的是,这种选择会使内容复杂化否则将是一个简单的 API。
@interface ViewController ()
@property (nonatomic) AVAudioEngine *audioEngine;
@property AVAudioFile *outputFile;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error;
if (![session setCategory:AVAudioSessionCategoryRecord error:&error]) {
NSLog(@"Failed to set session category: %@", error);
}
[[NSNotificationCenter defaultCenter] addObserver:self selectorselector(audioInterruptionHandler name:AVAudioSessionInterruptionNotification object:nil];
NSURL *outputURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0] URLByAppendingPathComponent"output.aac"];
__block BOOL outputFileInited = NO;
self.audioEngine = [[AVAudioEngine alloc] init];
AVAudioInputNode *inputNode = self.audioEngine.inputNode;
[inputNode installTapOnBus:0 bufferSize:512 format:nil block:^(AVAudioPCMBuffer *buffer, AVAudioTime * when) {
NSError *error;
if (self.outputFile == nil && !outputFileInited) {
NSDictionary *settings = @{
AVFormatIDKey: @(kAudioFormatMPEG4AAC),
AVNumberOfChannelsKey: @(buffer.format.channelCount),
AVSampleRateKey: @(buffer.format.sampleRate)
};
self.outputFile = [[AVAudioFile alloc] initForWritingutputURL settings:settings error:&error];
if (!self.outputFile) {
NSLog(@"output file error: %@", error);
abort();
}
outputFileInited = YES;
}
if (self.outputFile && ![self.outputFile writeFromBuffer:buffer error:&error]) {
NSLog(@"AVAudioFile write error: %@", error);
}
}];
if (![self.audioEngine startAndReturnError:&error]) {
NSLog(@"engine start error: %@", error);
}
// To stop recording, nil the outputFile at some point in the future.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"Finished");
self.outputFile = nil;
});
}
// https://developer.apple.com/library/archive/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/HandlingAudioInterruptions/HandlingAudioInterruptions.html
- (void)audioInterruptionHandlerNSNotification *)notification {
NSDictionary *info = notification.userInfo;
AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
switch (type) {
case AVAudioSessionInterruptionTypeBegan:
NSLog(@"Begin interruption");
break;
case AVAudioSessionInterruptionTypeEnded:
NSLog(@"End interruption");
// or ignore shouldResume if you're really keen to resume recording
AVAudioSessionInterruptionOptions endOptions = [info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
if (AVAudioSessionInterruptionOptionShouldResume == endOptions) {
NSError *error;
if (![self.audioEngine startAndReturnError:&error]) {
NSLog(@"Error restarting engine: %@", error);
}
}
break;
}
}
@end
注意您可能想要启用背景音频(当然,在 Info.plist 中添加 NSMicrophoneUsageDescription 字符串)。
关于ios - AVAudioRecorder - 如何从来电中恢复,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/54169447/
|