0

I am new to the iphone development. I am developing an app were i have to record voice and play back the recording, which i am doing properly but when I am getting any call at that movement only my recording is getting stop. I need to pause the recording and after the call is over agin the recording has to take place.

Please help me!

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
Sonu
  • 248
  • 1
  • 10
  • possible duplicate of [How to record and play sound in iPhone app?](http://stackoverflow.com/questions/5662297/how-to-record-and-play-sound-in-iphone-app) – Parth Bhatt Mar 12 '12 at 11:27
  • Nope. It isn't a duplicate, (s)he's referring to the recording being interrupted, not sound volume being too low as in that question. – Daniel G. Wilson Mar 12 '12 at 11:29
  • Search on google before directly posting questions here. – Parth Bhatt Mar 12 '12 at 11:29
  • yes you are right @xenElement. – Sonu Mar 12 '12 at 11:40
  • when ever iam getting interruption like getting phone call at that movement only my recording is getting stop.i dont want that .when ever there is an interruption the recorder have to pause ,after the interruption is over it has to start form the were i paused and continue the recording! – Sonu Mar 12 '12 at 11:45

2 Answers2

0

AVAudioRecorder class is used to record the audio.

Follow this Link it will help you to record and play

iphonecool
  • 99
  • 12
0

try this...

#import <AVFoundation/AVFoundation.h>
-(IBAction)record
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];

NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];
if(recordEncoding == ENC_PCM1)
{
    [recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
    [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
    [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
    [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];   

}
else
{
    NSNumber *formatObject;
    switch (recordEncoding) 
    {
        case (ENC_AAC1): 

            formatObject = [NSNumber numberWithInt: kAudioFormatMPEG4AAC];

            break;
        case (ENC_ALAC1):
            formatObject = [NSNumber numberWithInt: kAudioFormatAppleLossless];
            break;
        case (ENC_IMA41):
            formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
            break;
        case (ENC_ILBC1):
            formatObject = [NSNumber numberWithInt: kAudioFormatiLBC];
            break;
        case (ENC_ULAW1):
            formatObject = [NSNumber numberWithInt: kAudioFormatULaw];
            break;
        default:
            formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
    }
    [recordSettings setObject:formatObject forKey: AVFormatIDKey];
    [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
    [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
    [recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
    [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Recording"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; 
NSString *filePath =[NSString stringWithFormat:@"%@/%@.caf",dataPath,theDate];

NSURL *url = [NSURL fileURLWithPath:filePath];
// NSLog(@"===> %@",filePath);
app.audiofilepath=filePath;
NSError *error = nil;
audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
if ([audioRecorder prepareToRecord] == YES)
{
    [audioRecorder record];
}
else 
{
    int errorCode = CFSwapInt32HostToBig ([error code]); 
    //  NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode); 

}
}

 -(IBAction)playRecording
{
audiorec.enabled = NO;
audioplay.enabled = NO;
audiostop.enabled = YES;
audiodel.enabled = YES;

//NSLog(@"playRecording");
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

// NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *filename=theDate;
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Recording"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; 
NSString *filePath =[NSString stringWithFormat:@"%@/%@.caf",dataPath,filename];

NSURL *url = [NSURL fileURLWithPath:filePath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];

}
LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
Sanket Pandya
  • 1,095
  • 7
  • 21