2011-07-05 48 views
4

我的代理方法audioRecorderDidFinishRecordingaudioPlayerDidFinishPlaying未被調用。這些方法應該觸發一個'stopanimation'方法,在記錄結束後停止動畫。AVAudioPlayer和AVAudioRecorder:未調用代理方法

我在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]; 
} 

回答

3

您的代碼設置委託看起來很好。

您是否假設委託方法不僅僅是因爲動畫不停止或者您是否嘗試直接記錄/斷點方法而被調用?如果你沒有直接進行測試,那麼在假設它們不是的前提下進行測試。

如果您確認他們沒有被調用,很可能您的動畫綁定了self對象,因此它無法響應AV委託方法。

將動畫註釋掉,並在AV委託方法中添加一個日誌以查看它們是否被調用。

+0

我已經經歷了破發點檢查,並沒有在that.but停止當我在設備上它worked.i意味着委託方法稱爲 –

+0

謝謝你運行它!! ...你是對的.. –

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]); 
} 
1

檢查當前的AVAudioSession類別。如果該類別未設置爲AVAudioSessionRecord或AVAudioSessionPlayAndRecord,則記錄器將不起作用,也不會調用其任何委託方法。 AVAudioSession類別可以使用setCategory:error:方法進行更改。例如:

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