Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Create audioSession object,its upto you whether you want to activate or deactivate your app’s audio session.(<a href="https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html" rel="nofollow">AVAudioSession</a>)</p> <pre><code>audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&amp;err]; if(err) { NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]); return; } [audioSession setActive:YES error:&amp;err]; </code></pre> <p>Start recording proces,</p> <pre><code>-startRecording { [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey]; // We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality [recordSetting setValue:[NSNumber numberWithFloat:32000.0] forKey:AVSampleRateKey]; // We can use 2(if using additional h/w) or 1 (iPhone only has one microphone) [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey]; mediaPath = [[NSString stringWithFormat:@"%@/myVoice.mp3", DOCUMENTS_FOLDER] retain];//where you want to save your recorded audio. NSURL *url = [NSURL fileURLWithPath:mediaPath]; err = nil; NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&amp;err]; recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&amp;err]; [recorder setDelegate:self]; [recorder prepareToRecord]; recorder.meteringEnabled = YES; BOOL audioHWAvailable = audioSession.inputAvailable; if (! audioHWAvailable) { UIAlertView *cantRecordAlert = [[UIAlertView alloc] initWithTitle: @"Warning" message: @"Audio input hardware not available" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [cantRecordAlert show]; [cantRecordAlert release]; return; } [recorder record]; } </code></pre> <p>Pause recording:</p> <pre><code>-pauseRecording { [recorder pause]; //[recorder updateMeters]; } </code></pre> <p>Again resume the process..audiosession will do it for you...</p> <pre><code>-resumerecording { [recorder record]; //[recorder updateMeters]; } </code></pre> <p><strong>EDIT</strong>: [recorder <a href="https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVAudioRecorder_ClassReference/Reference/Reference.html#//apple_ref/occ/instm/AVAudioRecorder/updateMeters" rel="nofollow">updateMeters]</a>; should be called periodically which refreshes the average and peak power values for all channels of an audio recorder.</p> <p>You can use timer for that.</p> <p>For example:</p> <pre><code> NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateAudioDisplay) userInfo:NULL repeats:YES]; -(void)updateAudioDisplay { if (!recorder.isRecording) { [recorder updateMeters]; } else { if (recorder == nil) { //recording is not yet started } else { //paused } } } </code></pre> <p>You can download the sample code from <a href="https://app.box.com/s/ee2d09307146c9016f56" rel="nofollow">here</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload