2016-03-21 38 views
1

其實我想記錄音頻和存儲到文檔中,我找到了FilePath,甚至附加了音頻文件,但是我想要將實際的音頻附加到該路徑中,它不工作,我編寫該代碼在DidFinish這樣。AVAudioRecorderDelegate的代表沒有調用

- (void)viewDidLoad { 
     [super viewDidLoad]; 
     self.navigationItem.title = @"Recording Area"; 
     [btnRecord setBackgroundImage:[UIImage imageNamed:@"Record.png"] forState:UIControlStateNormal]; 

     session = [AVAudioSession sharedInstance]; 
     [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 

     audioRecorder.delegate = self; 


     NSDate *today = [[NSDate alloc]init]; 
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
     [dateFormatter setDateFormat:@"hh:mm:ss"]; 

     NSString *currentTime = [dateFormatter stringFromDate:today]; 

     NSString *outputPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0]; 

     outputPath = [outputPath stringByAppendingString:[NSString stringWithFormat:@"%@.m4a",currentTime]]; 

     NSMutableDictionary *recordDictionary = [[NSMutableDictionary alloc] init]; 

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


     audioRecorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:outputPath] settings:recordDictionary error:nil]; 

     audioRecorder.meteringEnabled = YES; 
     [audioRecorder prepareToRecord]; 

    } 

and For Recording Audio 

    -(IBAction)onClickRecord:(id)sender 
{ 

    if(player.playing) 
    { 
     [player stop]; 
    } 


    if([[btnRecord backgroundImageForState:UIControlStateNormal]isEqual:[UIImage imageNamed:@"Record.png"]]) 
    { 

     if (!audioRecorder.recording) 

     { 
      AVAudioSession *session = [AVAudioSession sharedInstance]; 
      [session setActive:YES error:nil]; 


      [audioRecorder record]; 
      [btnRecord setBackgroundImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal]; 
     } 



    } 

    else if([[btnRecord backgroundImageForState:UIControlStateNormal]isEqual:[UIImage imageNamed:@"Pause.png"]]) 
    { 
     [audioRecorder pause]; 

     [btnRecord setBackgroundImage:[UIImage imageNamed:@"Record.png"] forState:UIControlStateNormal]; 
    } 




} 

用於儲存錄制的音頻

-(IBAction)onClickSave:(id)sender 
{ 
    [audioRecorder stop]; 
    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
    [audioSession setActive:NO error:nil]; 


} 

最後也是真正的代表在哪裏我的代碼是不實際工作,我無法調用此方法

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag 
{ 


    [btnRecord setBackgroundImage:[UIImage imageNamed:@"Record.png"] forState:UIControlStateNormal]; 
    [arrayFiles addObject:recorder.url.absoluteString]; 
    NSLog(@"Array %@",arrayFiles); 

} 

所以請註明我在哪裏,我錯了。

+0

確保您audioRecorder有'strong'參考 –

+1

你設置你的錄音機委託前你真的創建它 – dan

+0

@property(強,非原子)AVAudioRecorder * audioRecorder在h文件中我創建了這個.. –

回答

2

初始化的AVAudioRecorder

移動之前,務必設置委託初始化後, audioRecorder.delegate = self;

audioRecorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:outputPath] settings:recordDictionary error:nil]; 
audioRecorder.meteringEnabled = YES; 
audioRecorder.delegate = self; 

[audioRecorder prepareToRecord]; 
+0

謝謝!你節省了很多時間... –

+0

感謝@dan,他是第一個觀察到的人) –