2012-03-12 165 views
0

我是iphone新手。我正在开发一个应用程序,我必须录制声音并播放录制内容,这样我才能正常工作,但是当我在该移动中收到任何电话时,只有我的录音停止。我需要暂停录音,并且在通话结束后录音必须进行。在iphone中录制和播放音频

请帮帮我!

+0

可能重复:// stackoverflow.com/questions/5662297/how-to-record-and-play-sound-in-iphone-app) – 2012-03-12 11:27:33

+0

没有。这不是重复的,他指的是录音被中断,而不是音量太低,因为在这个问题上。 – 2012-03-12 11:29:21

+0

在谷歌上搜索之前,直接在这里发布问题。 – 2012-03-12 11:29:46

回答

0

AVAudioRecorder类用于录制音频。

关注这个Link它会帮助您录制和播放

0

试试这个...

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

} 
【如何记录和iPhone应用程序播放声音?(HTTP的