In our iPhone application, provide the facility for the user to record voice and send the audio file as a
greeting to his friends via Email. Moreover, he can share the file with popular social networks like Twitter.
This is how we design the interface to record and playback voice. We use a UISlider as an progress indicator
for the user while he recording. The recorded audio file is in /tmp directory. After recording voice he can
play it or add the audio file as an attachment and send via Email.
NewRecordingUI.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
#import <MessageUI/MessageUI.h>
@interface NewRecordingUI : UIViewController <AVAudioRecorderDelegate,MFMailComposeViewControllerDelegate>
{
IBOutlet UIButton *playBtn;
IBOutlet UIButton *recBtn;
IBOutlet UILabel *recStstus;
BOOL isNotRecording;
NSURL *temporaryRecFile;
AVAudioRecorder *recorder;
NSTimer *sliderTimer;
}
@property(nonatomic,retain)IBOutlet UIButton *playBtn;
@property(nonatomic,retain)IBOutlet UIButton *recBtn;
@property(nonatomic,retain)IBOutlet UIButton *emailBtn;
@property(nonatomic,retain)IBOutlet UIButton *shareBtn;
@property (retain, nonatomic) IBOutlet UISlider *progressSlider;
-(IBAction)play;
-(IBAction)record;
-(IBAction)openMail:(id)sender;
@end
NewRecordingUI.m
#import "NewRecordingUI.h"
#import "GreetingAppViewController.h"
#import <MessageUI/MessageUI.h>
@implementation NewRecordingUI
@synthesize playBtn,recBtn,emailBtn,shareBtn,progressSlider;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
isNotRecording = YES;
playBtn.hidden = YES;
emailBtn.hidden = YES;
shareBtn.hidden = YES;
//[playBtn setEnabled:NO];
recStstus.text = @"";
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[audioSession setActive:YES error:nil ];
[super viewDidLoad];
}
- (void)dealloc
{
[playBtn release];
[recBtn release];
[emailBtn release];
[shareBtn release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidUnload
{
NSFileManager *fileHandler = [NSFileManager defaultManager];
[fileHandler removeItemAtPath:temporaryRecFile error:nil];
[recorder dealloc];
recorder = nil;
temporaryRecFile = nil;
playBtn.hidden = YES;
emailBtn.hidden = YES;
shareBtn.hidden = YES;
}
- (void)updateSlider
{
if (!isNotRecording) {
progressSlider.value = recorder.currentTime;
}
}
-(IBAction)record
{
if(isNotRecording)
{
sliderTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self selector:@selector(updateSlider)
userInfo:nil repeats:YES];
progressSlider.maximumValue = 100;
//[recorder recordForDuration:10];
isNotRecording = NO;
[recBtn setTitle:@"Stop" forState:UIControlStateNormal];
//[playBtn setEnabled:NO];
playBtn.hidden = YES;
emailBtn.hidden = YES;
shareBtn.hidden = YES;
recStstus.text = @"Recording...";
temporaryRecFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithString:@"VoiceFile"]]];
recorder = [[AVAudioRecorder alloc]initWithURL:temporaryRecFile settings:nil error:nil];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
}
else
{
isNotRecording = YES;
[recBtn setTitle:@"Record" forState:UIControlStateNormal];
playBtn.hidden = NO;
emailBtn.hidden = NO;
shareBtn.hidden = NO;
recStstus.text = @"";
[recorder stop];
progressSlider.value = 0;
}
}
-(IBAction)play
{
progressSlider.value = 0;
sliderTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self selector:@selector(updateSlider)
userInfo:nil repeats:YES];
recStstus.text = @"Playing...";
[recBtn setEnabled:NO];
NSLog(@"%@",temporaryRecFile);
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:temporaryRecFile error:nil];
player.volume = 120;
[player play];
}
-(IBAction)openMail:(id)sender
{
if([MFMailComposeViewController canSendMail])
{
//NSString *mediaFile = temporaryRecFile;
//NSURL *fileURL = [[NSURL alloc]initFileURLWithPath:mediaFile];
NSData *audioFile = [[NSData alloc]initWithContentsOfURL:temporaryRecFile];
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc]init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"Greeting Message"];
NSArray *toReceipients = [NSArray arrayWithObjects:@"rcb44u@gmail.com", nil];
[mailer setToRecipients:toReceipients];
[mailer addAttachmentData:audioFile mimeType:@"audio/caf" fileName:@"VoiceFile.caf"];
[mailer setMessageBody:@"My greetings for you" isHTML:NO];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Failure" message:@"Device does not support" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(IBAction)cancel
{
GreetingAppViewController * cancel= [[GreetingAppViewController alloc]initWithNibName:nil bundle:nil];
[self presentModalViewController:cancel animated:YES];
}
@end