2014-01-23 42 views
0

我想創建一個簡單的AVAudioRecorder包裝。 據我所知,在[recorder stop]之後,音頻文件會自動保存到路徑中。AVAudioRecorder無法保存到文件

但是在我的課上,它不會保存到路徑中。

控制檯日誌:

2014-01-23 15:34:55.754 AudioRecorderDemo[4496:60b] contentURL: file:///var/mobile/Applications/C20F5A5A-14A9-45D4-80A3-999976AC4055/Documents/MyAudio-someDemo.m4a 
2014-01-23 15:34:55.756 AudioRecorderDemo[4496:60b] fileExist? : no 

感謝您的幫助。

在GitHub上的類文件: AudioRecorder.h AudioRecorder.m

// Code when start: 

-(void)prepare 
{ 
    NSError *error; 

    // Setup audio session 
    AVAudioSession *session = [AVAudioSession sharedInstance]; 
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]; 

    if (error) 
    { 
     NSLog(@"Error in AVAudioSession: %@", error.description); 
     return; 
    } 

    // Define the recorder setting 
    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init]; 

    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey]; 
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
    [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; 

    // Initiate and prepare the recorder 
    NSURL *contentURL = [STLAudioRecorder contentsOfURLFromUniqueID:self.uniqueID fileNameFormat:self.fileNameFormat]; 
    self.recorder = [[AVAudioRecorder alloc] initWithURL:contentURL settings:recordSetting error:&error]; 

    if (error) 
    { 
     NSLog(@"Error in AVAudioRecorder: %@", error.description); 
     return; 
    } 

    self.recorder.delegate = self; 
    self.recorder.meteringEnabled = YES; 
    [self.recorder prepareToRecord]; 
} 

-(void)startRecording 
{ 
    if (!self.recorder) [self prepare]; 

    if (!self.recorder.isRecording) 
    { 
     AVAudioSession *session = [AVAudioSession sharedInstance]; 
     [session setActive:YES error:nil]; 
     [self.recorder record]; 
     NSLog(@"startRecording, url: %@", self.recorder.url.absoluteString); 
    } 
} 

// Code when stop: 

-(void)stopRecording 
{ 
    if (self.recorder && self.recorder.isRecording) 
    { 
     NSError *error; 
     [self.recorder stop]; 
     AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
     [audioSession setActive:NO error:&error]; 
     if (error) NSLog(@"Error in stopRecording: %@", error.description); 
    } 
    else 
    { 
     NSLog(@"stopRecording: !(self.recorder && self.recorder.isRecording)"); 
    } 
} 
+0

是音頻文件存儲在的contentURL路徑.. –

+0

2014年1月23日15:34:49.014 AudioRecorderDemo [4496:60b] startRecording,url:file:///var/mobile/Applications/C20F5A5A-14A9-45D4-80A3-999976AC4055/Documents/MyAudio-someDemo.m4a,路徑由a類方法 – siutsin

+0

我無法在.m文件中找到任何'viewDidLoad' – iCoder

回答

0

試試這個,

-(void) initAudioSession { 

AVAudioSession * audioSession = [AVAudioSession sharedInstance]; 

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error]; 

    [audioSession setActive:YES error: &error]; 
} 

-(void) recordStart { 

NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *docsDir = [dirPaths objectAtIndex:0]; 

tempFilePath = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:[NSString stringWithFormat: @"%@.%@",audioFileName, @"caf"]]]; 


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]; 
stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.%@",audioFileName, @"caf"]]]; 


recorder = [[ AVAudioRecorder alloc] initWithURL:tempFilePath settings:recordSetting error:&error]; 
[recorder setDelegate:self]; 

[recorder prepareToRecord]; 

[recorder record]; 
} 

-(void) stopRecording{ 
[recorder stop]; 
} 

-(void) playRecord{ 
avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:tempFilePath error:&error]; 
[avPlayer prepareToPlay]; 
[avPlayer play]; 
} 
+0

2014-01-23 16:07:13.831 AudioRecorderDemo [4541:60b] contentURL:file:/// var/mobile/Applications/C20F5A5A-14A9-45D4 -80A3-999976AC4055/Documents/someDemo.caf 2014-01-23 16:07:13.832 AudioRecorderDemo [4541:60b] fileExist? :不 – siutsin