繁体   English   中英

首次获取语音的iOS Mic检测失败

[英]IOS Mic detection for getting Voice fails first time IOS7

伙计们,我正在开发一个Ipad应用程序,在该应用程序中,我会播放3次哔声,然后检测声音。 但是问题在于,当我第一次进入阅读屏幕并开始阅读时,蜂鸣声很好,但麦克风无法检测到声音。 如果移至另一个屏幕并再次进入阅读屏幕,则蜂鸣声非常完美,麦克风也可以正常工作。 发出哔声的代码如下

   - (void)playAudio
    {
      [self setupAudioSession];
       NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"mp3"];
       NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
      AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    [fileURL release];
     self.click = newPlayer;
     [newPlayer release];
     [self.click setDelegate:self];
      [self.click  prepareToPlay];
     [self.click  play];
}

  - (void)setupAudioSession {
     static BOOL audioSessionSetup = NO;
      if (audioSessionSetup) {
         return;
     }
   [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;

    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);

   [[AVAudioSession sharedInstance] setActive: YES error: nil];

  audioSessionSetup = YES;

 }

在发出哔声之后,我调用了此功能以检测麦克风

-(void)startrecordingAfterCountdown{
        NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
        [self.view setUserInteractionEnabled:true];
         NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                          [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                          [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                          [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                          nil];

NSError *error;

   //    
   //    //    static BOOL audioSessionSetup2 = NO;
   //    //    if (audioSessionSetup2) {
   //    //         //return;
    //    //    }
    //       [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryRecord error: nil];
   //        UInt32 doSetProperty = 1;
   //    
   //       AudioSessionSetProperty kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
  //    
  //        [[AVAudioSession sharedInstance] setActive: YES error: nil];
  //    
  //       // audioSessionSetup2 = YES;
  //        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  //        [audioSession setActive:YES error:nil];
   //        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];

      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%@",[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.20){

    NSLog(@"%2f",lowPassResults);



}

}

请告诉我我该如何解决..该麦克风将在第一时间检测到语音

为了捕获音频,在调用prepareToRecord之前,您需要在代码中prepareToRecord

NSError *error; 
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&error];

进行呼叫后,请确保检查error对象,以防设置类别时出错。

大家好,我只是通过更改代码来解决此问题。 -(void)setupaudioSession && -(void)startrecordingAfterCountdown methods.

- (void)setupAudioSession {
 static BOOL audioSessionSetup = NO;
  if (audioSessionSetup) {
    // return;
 }
  [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
   UInt32 doSetProperty = 1;

  AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);

   [[AVAudioSession sharedInstance] setActive: YES error: nil];

    audioSessionSetup = YES;

 }


-(void)startrecordingAfterCountdown{
    NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
    [self.view setUserInteractionEnabled:true];
     NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                      [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                      [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                      [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                      [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                      nil];

     NSError *error;


        //    static BOOL audioSessionSetup2 = NO;
        //    if (audioSessionSetup2) {
       //         //return;
      //    }
     // [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryRecord error: nil];
  // UInt32 doSetProperty = 1;

 //AudioSessionSetProperty kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);

   // [[AVAudioSession sharedInstance] setActive: YES error: nil];

   // audioSessionSetup2 = YES;
      AVAudioSession *audioSession = [AVAudioSession sharedInstance];
      [audioSession setActive:YES error:nil];
       [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];

     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%@",[error description]);
     }
 }

暂无
暂无

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

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