Controlling Recording
With the soundRecorder allocated and initialized, all that we need to do is implement recordAudio: so that the record and stop methods are invoked as needed. To make things interesting, we'll have the recordButton change its title between Record Audio and Stop Recording when pressed.
Add the following code in Listing-5 to MediaPlaygroundViewController.m.
LISTING-5
1: -(IBAction)recordAudio:(id)sender { 2: if ([recordButton.titleLabel.text isEqualToString:@"Record Audio"]) { 3: [soundRecorder record]; 4: [recordButton setTitle:@"Stop Recording" 5: forState:UIControlStateNormal]; 6: } else { 7: [soundRecorder stop]; 8: [recordButton setTitle:@"Record Audio" 9: forState:UIControlStateNormal]; 10: } 11: }
In line 2, the method checks the title of the recordButton variable. If it is set to Record Audio, the method uses [soundRecorder record] to start recording (line 3), and then, in lines 4-5, sets the recordButton title to Stop Recording. If the title doesn't read Record Audio, then we're already in the process of making a recording. In this case, we use [soundRecorder stop] in line 7 to end the recording and set the button title back to Record Audio in lines 8-9.
That's it for recording! Let's implement playback so that we can actually hear what we've recorded!
Controlling Audio Playback
To play back the audio that we recorded, we'll create an instance of the AVAudioPlayer class, point it at the sound file we created with the recorder, and then call the play method. We'll also add the method audioPlayerDidFinishPlaying:successfully: defined by the AVAudioPlayerDelegate protocol so that we've got a convenient place to release the audio player object.
Start by adding the playAudio: method, in Listing-6, to MediaPlaygroundViewController.m.
LISTING-6
1: -(IBAction)playAudio:(id)sender { 2: NSURL *soundFile; 3: NSString *tempDir; 4: AVAudioPlayer *audioPlayer; 5: 6: tempDir=NSTemporaryDirectory(); 7: soundFile=[NSURL fileURLWithPath: 8: [tempDir stringByAppendingString:@"sound.caf"]]; 9: 10: audioPlayer = [[AVAudioPlayer alloc] 11: initWithContentsOfURL:soundFile error:nil]; 12: 13: [audioPlayer setDelegate:self]; 14: [audioPlayer play]; 15: }
In lines 2-3, we define variables for holding the iPhone application's temporary directory and a URL for the sound file-exactly the same as the record.
Line 4 declares the audioPlayer instance of AVAudioPlayer.
Lines 6-8 should look familiar because once again, they grab and store the temporary directory and use it to initialize an NSURL object, soundFile, that points to the sound.caf file we've recorded.
In lines 10 and 11, the audio player, audioPlayer, is allocated and initialized with the contents of soundFile.
Line 13 is a bit out of the ordinary, but nothing too strange. The setDelegate method is called with the parameter of self. This tells the audioPlayer instance that it can look in the view controller object (MediaPlaygroundViewController) for its AVAudioPlayerDelegate protocol methods.
Line 14 initiates playback using the play method.
Handling Cleanup
To handle releasing the AVAudioPlayer instance after it has finished playing, we need to implement the protocol method audioPlayerDidFinishPlaying:successfully:. Add the following method code to the view controller implementation file:
(void)audioPlayerDidFinishPlaying: (AVAudioPlayer *)player successfully:(BOOL)flag { [player release]; }
We get a reference to the player we allocated via the incoming player parameter, so we just send it the release message and we're done!
Choose Build and Run in Xcode to test recording and playback. Press Record Audio to begin recording. Talk, sing, or yell at your iPhone. Touch Stop Recording, to end the recording. Finally, press the Play Audio button to initiate playback.
It's time to move on to the next part of this lesson exercise: accessing and displaying photos from the photo library.