Record sound in iPhone


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. -(void) mic{
  2.  
  3. [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
  4. time=0;
  5.  
  6. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  7. NSString *documentsDirectory = [paths objectAtIndex:0];
  8. NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
  9. NSString *caldate = [now description];
  10. NSString *recorderFilePath = [[NSString stringWithFormat:@"%@/%@.caf", documentsDirectory, caldate] retain];
  11.  
  12. NSURL *url = [NSURL fileURLWithPath:recorderFilePath];
  13. NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
  14. [NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
  15. [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
  16. [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
  17. [NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
  18. nil];
  19.  
  20. NSError *error;
  21.  
  22. recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
  23.  
  24. if (recorder) {
  25. [recorder prepareToRecord];
  26. recorder.meteringEnabled = YES;
  27. [recorder record];
  28. levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
  29. }
  30. }
  31.  
  32. - (void)levelTimerCallback:(NSTimer *)t {
  33. [recorder updateMeters];
  34. isTimerValid=YES;
  35. if([recorder averagePowerForChannel:0]>-25){
  36. //record in progress
  37. }
  38. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.