【问题标题】:Why my iOS device not detecting my voice?为什么我的 iOS 设备检测不到我的声音?
【发布时间】:2015-09-24 13:31:25
【问题描述】:

在我的视图中我写了

- (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 设备上运行我的应用程序时,它没有检测到任何声音,尽管我在“设置”中为我的应用打开了允许麦克风

我该怎么办?

【问题讨论】:

  • 我编辑了我的问题@Raptor
  • 是否允许[NSURL fileURLWithPath:@"/dev/null"]?我怀疑。

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


【解决方案1】:

请在您的 viewDidLoad 中使用以下这些行

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

这适用于您的 iOS 设备。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-20
    • 2016-02-04
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多