【问题标题】:How to tell that AVAudioPlayer has finished playing如何判断 AVAudioPlayer 播放完毕
【发布时间】:2014-09-02 16:07:23
【问题描述】:

我正在开发一个使用AVAudioPlayer 播放声音的应用,我想知道声音何时播放完毕。我想在声音停止播放时更改图像

这是我用来创建播放器的代码:

NSURL* url = [[NSBundle mainBundle] URLForResource:@"LOLManFace1"  withExtension:@"mp3"];
NSAssert(url, @"URL is valid.");
NSError* error = nil;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&url];error;

if(!self.player)
{
    NSLog(@"Error creating player: %@", error);
}

这是我用来播放声音的代码:

[self.player play];

如何判断播放已结束?

非常感谢您!

【问题讨论】:

    标签: ios objective-c ios7 xcode5 avaudioplayer


    【解决方案1】:

    查看 AVAudioPlayerDelegate 协议,它有一个方法可以在音频播放完成时告诉代理,特别是您要查找的是 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag,这里是 reference

    希望对你有帮助

    丹尼尔

    【讨论】:

    • 嗨,感谢您的回答,我在按钮操作上调用 [self audioPlayerDidFinishPlaying:self.player:YES],但在 if 语句中,我想知道声音是否结束,但是错误跳转我怎么能把它放在一个如果能够改变图像?
    • 你没有正确使用这个,参考developer.apple.com/library/ios/documentation/Cocoa/Conceptual/…查看协议和委托的工作流程
    【解决方案2】:

    很久以前,我很确定您曾经想通了。如果有人看到这篇文章:

    1) 你的类应该实现 AVAudioPlayerDelegate。 (https://developer.apple.com/documentation/avfoundation/avaudioplayerdelegate)

    其中一个委托方法是:

    audioPlayerDidFinishPlaying:successfully:
    

    2) 设置委托:

    self.player.delegate = self
    

    【讨论】:

      猜你喜欢
      • 2011-04-25
      • 1970-01-01
      • 2012-07-27
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多