gpt4 book ai didi

ios - AVAudioRecorder - 如何从来电中恢复

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:20:22 31 4
gpt4 key购买 nike

我正在努力实现我认为会非常容易但事实并非如此的事情。

我正在玩“react-native-audio”库的源代码。但是你可以假设我是为了这个问题而在本地工作。

这是一个reference for the source code我在玩。

我的目标很简单,我正在使用 AVAudioRecorder 来录制 session (大约需要 30 分钟)。如果在录音过程中有来电,我希望我的应用能够通过执行以下选项之一“恢复”:

1) 在“来电”时“暂停”记录,当应用程序返回前台时“恢复”。

2) 来电时 - 关闭当前文件,当应用程序返回前台时,使用新文件开始新录音(第 2 部分)。

显然选项 (1) 是首选。

请注意,我很清楚使用 AVAudioSessionInterruptionNotification 并在我的实验中使用它,但到目前为止运气不佳,例如:

- (void) receiveAudioSessionNotification:(NSNotification *) 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];
}
}
}

请注意,我会为这个问题设立悬赏金,但唯一可接受的答案是针对真实世界的工作代码,而不是“理论上应该工作”的代码。非常感谢您的帮助:)

最佳答案

我选择了AVAudioEngineAVAudioFile作为解决方案,因为代码很简短,AVFoundation的中断处理是particularly simple (您的播放器/录音机对象已暂停,取消暂停会重新激活您的 Audio Session )。

N.B AVAudioFile 没有明确的关闭方法,而是在 dealloc 期间写入 header 并关闭文件,遗憾的是,这个选择使事情复杂化否则将是一个简单的 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 selector:@selector(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] initForWriting:outputURL 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)audioInterruptionHandler:(NSNotification *)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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com