averagePowerForChannel使用iOSimulator但不在iPad

时间:2015-06-14 17:48:43

标签: ios ipad

在我的iOS 8.0小应用程序中,我测量麦克风的输入电平,如果它大于-40 dB,我在UIImageView中更改图像并播放mp3文件。它在iOSimulator中运行良好,但在iOS iOS 8.0上无效。 (麦克风适用于app)。问题是什么? 感谢。

//  ViewController.m

#import "ViewController.h"
#import "AVFoundation/AVAudioPlayer.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()
@end

@implementation ViewController

NSTimer *meterTimer;
AVAudioRecorder *recorder;
AVAudioPlayer *player;
NSString *audioFilePath; NSURL *pathAsURL;   // for audioplayer

- (void)viewDidLoad
 {   [super viewDidLoad];

meterTimer = [NSTimer scheduledTimerWithTimeInterval:0.3    // sets timer interrupt
target:self selector:@selector(timerArrived) userInfo:nil repeats:YES];

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithFloat: 44100.0],AVSampleRateKey,
                          [NSNumber numberWithInt: kAudioFormatAppleLossless],AVFormatIDKey,
                          [NSNumber numberWithInt: 1],AVNumberOfChannelsKey,
                          [NSNumber numberWithInt: AVAudioQualityMax],AVEncoderAudioQualityKey,nil];
NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:[NSTemporaryDirectory()
            stringByAppendingPathComponent:@"tmp.caf"]]  settings:settings error:&error];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
}

- (void)timerArrived    // called by timer
{   int intPower; float fValue;
    [recorder updateMeters];    // check input level
    fValue = [recorder averagePowerForChannel:0];
    intPower = roundf(fValue);
    if (intPower > -40)
{   _microphImage.image = [UIImage imageNamed:@"Microphone2.png"];
    audioFilePath = [[NSBundle mainBundle] pathForResource:@"aga" ofType:@"mp3"];
    pathAsURL = [[NSURL alloc] initFileURLWithPath:audioFilePath]; NSError *error;
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:pathAsURL error:&error];
    [player play];
}
else {_microphImage.image = [UIImage imageNamed:@"Microphone.png"];}
}

- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];} 
@end

0 个答案:

没有答案