简体   繁体   中英

IOS Mic detection for getting Voice fails first time IOS7

Hay guys I am developing an Ipad app in which i play beep sound 3 times and then detecting voice. But problem is that when first time i am at my reading screen and start reading then beep sound is fine but mic dose not detect voice. if move to another screen and again come to reading screen then beep sound is perfect and mic is also working fine. the code for play beep sound is as follow

   - (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;

 }

and after playing beep then i called this function for detecting mic

-(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);



}

}

please tell me how i can fix this..that mic will detect voice first time

In order to capture audio, you need this in your code before you call prepareToRecord :

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

Make sure to check the error object after making the call in case there's an error setting the category.

Hey guys I just fixed the issue by changing my code. -(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]);
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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