繁体   English   中英

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

[英]Why my iOS device not detecting my voice?

在我写的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设备。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM