How to record voice programatically and store on the server in iphone

H

CoreAudio.framework
AVFoundation.framework
———————————————————————————————————
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
Add delegate
@interface VoiceRecord : UIViewController <AVAudioRecorderDelegate>
———————————————————————————————————-
-(void) recordstart
{
AVAudioSession * audioSession = [AVAudioSession sharedInstance];

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
[audioSession setActive:YES error: &error];

NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @”%.0f.%@”, [NSDate timeIntervalSinceReferenceDate] *     1000.0, @”abc.mov”]]];
NSLog(@”Using File called: %@”,recordedTmpFile);

recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder record];
}
————————————————————————————————————-
or if you want to Store on server then use below more code
-(void) Storeonserver
{
file1Data = [NSData dataWithContentsOfFile:[recordedTmpFile path]];
//Create web Service
NSString *urlString = @”http://demos.nanostuffs.com/iphone/audio.php”;

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@”POST”];

NSString *boundary = [NSString stringWithString:@”—————————14737809831466499882746641449″];
NSString *contentType = [NSString stringWithFormat:@”multipart/form-data; boundary=%@”,boundary];
[request addValue:contentType forHTTPHeaderField: @”Content-Type”];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@”rn–%@rn”,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@”Content-Disposition: form-data; name=”userfile”; filename=”abc.mov”rn”]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@”Content-Type: application/octet-streamrnrn”] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:file1Data]];
[body appendData:[[NSString stringWithFormat:@”rn–%@–rn”,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];
NSLog(@”Return String= %@”,returnString);
}
——————————————————————————————————–
//Play Recording
-(void) playback
{
//Play recoding from server
NSData *mydata = [NSData dataWithContentsOfURL:[NSURL URLWithString:@”http://demos.nanostuffs.com/Anroid/audio/abc.mov”]];
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithData:mydata error:nil];
[avPlayer prepareToPlay];
[avPlayer play];

//or use this code for latest recoding and play
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
NSLog(@”%@”, recordedTmpFile);
[avPlayer prepareToPlay];
[avPlayer play];
}

About the author

mayur.bhansali
By mayur.bhansali

Category