【问题标题】:AVAudioPlayer and AVAudioRecorder: Delegate Methods not CalledAVAudioPlayer 和 AVAudioRecorder:未调用委托方法
【发布时间】:2011-07-05 07:32:29
【问题描述】:

我的委托方法 audioRecorderDidFinishRecordingaudioPlayerDidFinishPlaying 没有被调用。这些方法应该触发一个“stopanimation”方法,在录制完成后停止动画。

我已经在audioPlayerDidFinishPlaying 的末尾调用了stopanimation 方法。

这是分配委托的相关代码:

   VoiceEditor.h

    @interface VoiceEditor : UIViewController <UITextFieldDelegate, AVAudioRecorderDelegate, AVAudioPlayerDelegate>{    
}

VoiceEditor.m

- (void)record{
    recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:pathString] settings:recordSettings error:nil];
    [recorder setDelegate:self];
    [recorder record];
    recording = YES;        
    [pauseButton setImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateNormal];
    [pauseButton setEnabled:YES];
    [playButton setEnabled:NO];
    [recordButton setEnabled:NO];  
    [self beginAnimation];
}

- (void)play{              
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathString] error:nil];
    double seconds=[player duration];
    NSLog(@"%f",seconds);
    [player setDelegate:self];
    [player play];
    playing = YES;              
    [recordButton setEnabled:NO];
    [pauseButton setEnabled:YES];
    [playButton setEnabled:NO];     
    [self beginAnimation];
}

【问题讨论】:

    标签: iphone ios4 delegates avaudioplayer avaudiorecorder


    【解决方案1】:

    您设置委托的代码看起来不错。

    您是否假设仅因为动画没有停止而没有调用委托方法,或者您是否尝试直接记录/断点方法?如果您没有直接测试,请在假设它们不是之前进行测试。

    如果您已确认它们没有被调用,那么您的动画很可能会占用 self 对象,使其无法响应 AV 委托方法。

    注释掉动画,然后在 AV 委托方法中添加一个日志,看看它们是否被调用。

    【讨论】:

    • 我已经检查了断点,但它并没有停止。但是当我在设备上运行它时它起作用了。我的意思是调用的委托方法
    【解决方案2】:

    尝试传递错误参数以确保您的记录器对象被正确创建。

    NSError *err=nil;
    recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:pathString] settings:recordSettings error:&err];
    if(!recorder){
        NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    }
    

    【讨论】:

      【解决方案3】:

      检查当前的 AVAudioSession 类别。如果类别未设置为 AVAudioSessionRecord 或 AVAudioSessionPlayAndRecord,则记录器将无法工作,并且不会调用其委托方法。可以使用 setCategory:error: 方法更改 AVAudioSession 类别。例如:

      NSError *sessionCategoryError = nil;
      AVAudioSession *audioSession = [AVAudioSession sharedInstance];
      BOOL success = [audioSession setCategory: AVAudioSessionCategoryPlayAndRecord
                                         error: &sessionCategoryError];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-12
        • 1970-01-01
        • 1970-01-01
        • 2015-06-09
        相关资源
        最近更新 更多