麦克风获取声音方法未调用



我想在iPhone麦克风收到一些声音时调用委托方法。 呼叫未呼叫的记录器代理。 我想得到声音强度。

我已经尝试过这段代码。 但不起作用。

NSArray *pathComponents = [NSArray arrayWithObjects:
                               [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                               @"MyAudioMemo.m4a",
                               nil];
    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
    // Setup audio session
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    // Define the recorder setting
    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
    // Initiate and prepare the recorder
    recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
    recorder.delegate = self;
    recorder.meteringEnabled = YES;
    [recorder prepareToRecord];

    [recorder record];
    [recorder updateMeters];
    level = [recorder peakPowerForChannel:0];

    [recorder stop];
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setActive:NO error:nil];


    // Do any additional setup after loading the view, typically from a nib.
}

当用户听到声音时调用此方法...

-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{

    NSLog(@"Sound level is  %f",level);
}

我该怎么做,提前谢谢。

实际上,您的记录器同时启动和停止,这就是其委托从不触发的原因。如果我正确理解了您想要什么,那么这就是解决方案。

NSArray *pathComponents = [NSArray arrayWithObjects:
                           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                           @"MyAudioMemo.m4a",
                           nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
// Define the recorder setting
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
// Initiate and prepare the recorder
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];

//[recorder record];  // Do not start recording here
[recorder updateMeters];
level = [recorder peakPowerForChannel:0];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
  self.meterTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
                                 target:self
                               selector:@selector(updateAudioMeter)
                               userInfo:nil
                                repeats:YES];
 }
    // Do any additional setup after loading the view, typically from a nib.
 }

在此处获取您的计量水平

  -(void)updateAudioMeter
    {
     [self.recorder updateMeters];
      self.dBLevel = [self.recorder averagePowerForChannel:0];
       if(self.dBLevel>-25)
         {
           [self.recorder record];
        }
        else 
          { 
             [self.recorder stop];
          } 
  }

最新更新