使用 AVAudioRecorder 处理中断
Handling Interruptions With AVAudioRecorder
我正在使用 AVAudioRecorder 检查音量级别并相应地更新 UIImageView。但是,当我为 Siri 按住主页按钮或设备收到 phone 呼叫时,UIImageView 不再更新。这是我在 .h 中导入的当前代码:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
然后我使用这段代码创建了记录器并更新了 .m 中的 UIImageView:
@interface MyViewController ()
@property NSTimer *checkDecibelLevelsTimer;
@property (strong, nonatomic) IBOutlet UIImageView *visualizer;
@property AVAudioRecorder *recorder;
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
[audioSession setActive:YES error:nil];
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (self.recorder) {
[self.recorder prepareToRecord];
self.recorder.meteringEnabled = YES;
[self.recorder record];
} else {
NSLog(@"%@", [error description]);
}
self.checkDecibelLevelsTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03
target: self
selector: @selector(checkDecibelLevelsCallback:)
userInfo: nil
repeats: YES];
}
-(void)checkDecibelLevelsCallback:(NSTimer *)timer {
[self.recorder updateMeters];
if ([self.recorder averagePowerForChannel:0] < -40) {
self.visualizer.image = [UIImage imageNamed:@"Visualizer1"];
} else if ([self.recorder averagePowerForChannel:0] < -35) {
self.visualizer.image = [UIImage imageNamed:@"Visualizer2"];
} else if ([self.recorder averagePowerForChannel:0] < -30) {
self.visualizer.image = [UIImage imageNamed:@"Visualizer3"];
} else if ([self.recorder averagePowerForChannel:0] < -25) {
self.visualizer.image = [UIImage imageNamed:@"Visualizer4"];
} else if ([self.recorder averagePowerForChannel:0] < -20) {
self.visualizer.image = [UIImage imageNamed:@"Visualizer5"];
} else if ([self.recorder averagePowerForChannel:0] < -15) {
self.visualizer.image = [UIImage imageNamed:@"Visualizer6"];
} else if ([self.recorder averagePowerForChannel:0] < -10) {
self.visualizer.image = [UIImage imageNamed:@"Visualizer7"];
} else {
self.visualizer.image = [UIImage imageNamed:@"Visualizer8"];
}
}
我试图通过在 .m 中实现此代码来处理中断:
- (void)beginInterruption {
NSLog(@"begin interruption");
[self.recorder pause];
}
- (void)endInterruption {
NSLog(@"end interruption");
[self.recorder record];
}
并在 .h 中添加 @interface MyViewController : UIViewController <AVAudioSessionDelegate>
。我注意到 NSLog returns 什么都没有,UIImageView 仍然没有更新。我该如何解决这个问题?
好的,我想出了解决办法。我把它放在 viewDidLoad 方法中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willResignInactive) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
并添加了以下方法:
- (void)willResignInactive {
[self.recorder stop];
}
- (void)didBecomeActive {
[self.recorder record];
}
我正在使用 AVAudioRecorder 检查音量级别并相应地更新 UIImageView。但是,当我为 Siri 按住主页按钮或设备收到 phone 呼叫时,UIImageView 不再更新。这是我在 .h 中导入的当前代码:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
然后我使用这段代码创建了记录器并更新了 .m 中的 UIImageView:
@interface MyViewController ()
@property NSTimer *checkDecibelLevelsTimer;
@property (strong, nonatomic) IBOutlet UIImageView *visualizer;
@property AVAudioRecorder *recorder;
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
[audioSession setActive:YES error:nil];
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
NSError *error;
self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (self.recorder) {
[self.recorder prepareToRecord];
self.recorder.meteringEnabled = YES;
[self.recorder record];
} else {
NSLog(@"%@", [error description]);
}
self.checkDecibelLevelsTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03
target: self
selector: @selector(checkDecibelLevelsCallback:)
userInfo: nil
repeats: YES];
}
-(void)checkDecibelLevelsCallback:(NSTimer *)timer {
[self.recorder updateMeters];
if ([self.recorder averagePowerForChannel:0] < -40) {
self.visualizer.image = [UIImage imageNamed:@"Visualizer1"];
} else if ([self.recorder averagePowerForChannel:0] < -35) {
self.visualizer.image = [UIImage imageNamed:@"Visualizer2"];
} else if ([self.recorder averagePowerForChannel:0] < -30) {
self.visualizer.image = [UIImage imageNamed:@"Visualizer3"];
} else if ([self.recorder averagePowerForChannel:0] < -25) {
self.visualizer.image = [UIImage imageNamed:@"Visualizer4"];
} else if ([self.recorder averagePowerForChannel:0] < -20) {
self.visualizer.image = [UIImage imageNamed:@"Visualizer5"];
} else if ([self.recorder averagePowerForChannel:0] < -15) {
self.visualizer.image = [UIImage imageNamed:@"Visualizer6"];
} else if ([self.recorder averagePowerForChannel:0] < -10) {
self.visualizer.image = [UIImage imageNamed:@"Visualizer7"];
} else {
self.visualizer.image = [UIImage imageNamed:@"Visualizer8"];
}
}
我试图通过在 .m 中实现此代码来处理中断:
- (void)beginInterruption {
NSLog(@"begin interruption");
[self.recorder pause];
}
- (void)endInterruption {
NSLog(@"end interruption");
[self.recorder record];
}
并在 .h 中添加 @interface MyViewController : UIViewController <AVAudioSessionDelegate>
。我注意到 NSLog returns 什么都没有,UIImageView 仍然没有更新。我该如何解决这个问题?
好的,我想出了解决办法。我把它放在 viewDidLoad 方法中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willResignInactive) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
并添加了以下方法:
- (void)willResignInactive {
[self.recorder stop];
}
- (void)didBecomeActive {
[self.recorder record];
}