1

我正在使用 AVAudioRecorder 来检查音量并相应地更新 UIImageView。但是,当我按住 Siri 的主页按钮或设备接到电话时,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];
}

并添加@interface MyViewController : UIViewController <AVAudioSessionDelegate>.h。我注意到 NSLog 没有返回任何内容,并且 UIImageView 仍然没有更新。我怎样才能解决这个问题?

4

1 回答 1

0

好吧,我想出了一个解决方案。我把它放在 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];
}
于 2015-07-31T02:31:43.390 回答