为什么我的iOS设备没有检测到我的声音?

时间:2015-07-07 07:18:55

标签: ios iphone ipad cocos2d-iphone ios-simulator

在我的viewdidload中我写了

- (void)viewDidLoad 
{
    [super viewDidLoad];

    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;
    recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];

    if (recorder) {
        [recorder prepareToRecord];
        recorder.meteringEnabled = YES;
        [recorder record];
        levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 
                                                      target:self 
                                                    selector:@selector(levelTimerCallback:) 
                                                    userInfo:nil 
                                                     repeats:YES];
    } else {
        NSLog(@"%@",[error description]);
    }
}

另一种实现方法是

- (void)levelTimerCallback:(NSTimer *)timer 
{
    [recorder updateMeters];

    const double ALPHA = 0.05;
    double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
    lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;  

    if (lowPassResults < 0.95)
        NSLog(@"Mic blow detected");
    else
        NSLog(@"Person spoked");
}

当我使用我的Mac麦克风在模拟器上运行我的代码时,它可以工作,当我说话时,它出现在“人员说话”的情况下,但是当我在真正的IOS设备上运行我的应用程序时,它没有检测到任何声音,尽管我已经在“设置”

中为我的应用启用了micro microPhone

我该怎么办?

1 个答案:

答案 0 :(得分:3)

请在viewDidLoad中使用以下这一行

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

这适用于您的iOS设备。