audioPlayerDidFinishPlaying:successfully: 未调用

audioPlayerDidFinishPlaying:successfully: not called

我正在尝试 segue 在一个简短的音乐剪辑结束时转到下一个视图。没有任何错误,但是当我 运行 它时, NSLog 消息永远不会出现并且 segue 永远不会发生。为什么 delegate 不知道音频何时结束?

这是 .h 文件:

@interface RecordingViewController : UIViewController 
<AVAudioPlayerDelegate> {

    AVAudioPlayer *player;
    NSArray *recordingsArray;
    int randomizedArtist;

}

这是来自 viewDidLoad 的一些代码:

// set self as AVAudioPlayerDelegate
[player setDelegate:self];

// prep Audio Player
@try {
    NSError *myError;

    path = [[NSBundle mainBundle] pathForResource:recordingsArray[randomizedArtist] ofType:@"mp3"];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]  error:&myError];

    if (!player) {
        NSLog(@"viewDidLoad:error %@", [myError localizedDescription]);
        NSLog(@"viewDidLoad:failure reason %@", [myError localizedFailureReason]);
        NSLog(@"viewDidLoad:recovery option %@", [myError localizedRecoveryOptions]);
        NSLog(@"viewDidLoad:recovery suggestions %@", [myError localizedRecoverySuggestion]);
    }
}
@catch (NSException *exception){

    NSLog(@"Exception %@ occurred", exception.name);
    NSLog(@"Exception reason %@", exception.reason);
    NSLog(@"Exception userInfo %@", exception.userInfo);

    // make pop up alert

}
@finally{

    NSLog(@"Finally on reading URL file called");

}

[player prepareToPlay];

这里是我调用播放器开始播放的地方:

- (IBAction)playButton:(id)sender {
    [player play];
}

这里是我调用 segue 的地方:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    NSLog(@"Player did finish playing");
    [self performSegueWithIdentifier:@"quizVCSegue" sender:self];
}

检查此代码是否有效

@interface ViewController()..,AVAudioPlayerDelegate

分配 AVPlayer

 NSError *error;
_audioPlayerRecord = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&error];
if (error)
{
    NSLog(@"Error in audioPlayer: %@",
          [error localizedDescription]);
} else {
    _audioPlayerRecord.delegate = self;
    [_audioPlayerRecord prepareToPlay];
}

#import <AVFoundation/AVFoundation.h>