完成音频播放后未调用AVPlayerItemDidPlayToEndTimeNotification

时间:2014-10-09 03:03:57

标签: ios xcode

我不能为我的生活找出为什么我无法检测我的声音何时完成播放。我可以让它为视频工作没问题。我一直在搜索过去两个小时,但我遇到的解决方案似乎都没有。

声音很好。我只需要知道它何时结束播放。

·H

@interface soundController : UIViewController <AVAudioPlayerDelegate> {

    AVAudioPlayer *audioPlayer;

}

- (IBAction) soundButtonClicked;
- (void) soundDidStop:(NSNotification *)notification;

的.m

- (IBAction) soundButtonClicked {

    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/my_sound.mp3", [[NSBundle mainBundle] resourcePath]]];  
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = 0;


    [[NSNotificationCenter defaultCenter] addObserver:self
                          selector:@selector(soundDidStop:)
                          name:AVPlayerItemDidPlayToEndTimeNotification
                          object:audioPlayer];
    [audioPlayer play];

}


- (void) soundDidStop:(NSNotification *)notification {

    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@""
                          message:@"sound stopped"
                          delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
    [alert show];
    [alert release];


}

非常感谢任何见解。谢谢!

1 个答案:

答案 0 :(得分:1)

AVPlayerItemDidPlayToEndTimeNotification`是AVPlayerItem可用的通知,这不是您在代码中使用的通知。

你正在使用AVAudioPlayer,这是一个完全不同的对象,界面完全不同。

在您的情况下,您可以使用AVAudioPlayerDelegate协议。您要捕获的委托方法是audioPlayerDidFinishPlaying:successfully: