用于获取语音的IOS Mic检测第一次IOS7失败

时间:2013-10-24 05:36:31

标签: ios iphone objective-c ios7

  

Hay家伙我正在开发一个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);



}

}

  

请告诉我如何解决这个问题......麦克风会第一次检测到语音

2 个答案:

答案 0 :(得分:0)

为了捕获音频,您需要先在代码中使用此功能,然后再拨打prepareToRecord

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

确保在拨打电话后检查error对象,以防设置类别时出错。

答案 1 :(得分:0)

嘿伙计我刚刚修改了我的代码来解决这个问题。-(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]);
     }
 }