0

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

}

我怎么能这样做提前谢谢。

4

1 回答 1

0

实际上,您的记录器同时启动和停止,这就是它的代表永远不会触发的原因。如果我正确理解了您想要的内容,那么这就是解决方案。

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];
          } 
  }
于 2013-10-24T09:41:37.867 回答