RSS

Tag Archives: voice record

Record and playback voice in iPhone

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];

}

- (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
 
2 Comments

Posted by on May 28, 2012 in iphone

 

Tags: ,