AVAudioRecorderDelegate 的代表没有调用

Posted

技术标签:

【中文标题】AVAudioRecorderDelegate 的代表没有调用【英文标题】:Delegates Of AVAudioRecorderDelegate are Not Calling 【发布时间】:2016-03-21 13:01:47 【问题描述】:

实际上我想录制音频并存储到文档中,我已经找到了 FilePath,甚至附加了音频文件,但我想将实际音频附加到该路径中,它不起作用,我在 DidFinish 中编写了该代码,如下所示.

    - (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);


所以请指出我哪里错了。

【问题讨论】:

确保您的 audioRecorder 有 strong 参考 您在实际创建之前在录音机上设置了代理 @property (strong,nonatomic) AVAudioRecorder *audioRecorder in h 文件我已经创建了这个.. 【参考方案1】:

您在初始化 AVAudioRecorder 之前设置委托

移动 audioRecorder.delegate = self; 初始化后

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

[audioRecorder prepareToRecord];

【讨论】:

感谢@dan,他是第一个观察到这一点的人)

以上是关于AVAudioRecorderDelegate 的代表没有调用的主要内容,如果未能解决你的问题,请参考以下文章

在tableView内部创建tableView

Swift 错误:类 RecordSoundsViewController 没有初始化器

IOS开发实现录音功能

IOS开发之 一个实例解析 录音

关于光的反射和折射的例子?

既是3的倍数又是5的倍数都有哪些