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

在我写的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设备上运行我的应用程序时,虽然我已经打开了它但它没有检测到任何声音在“设置”中为我的应用程序启用microPhone

我该怎么办?

解决方法

请在viewDidLoad中使用以下这些行

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

这适用于您的iOS设备.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...