【发布时间】:2017-03-13 02:56:22
【问题描述】:
我正在尝试在 Swift 操场上播放完歌曲时调用一个函数。这是我正在使用的代码:
NotificationCenter.default.addObserver(self, selector: Selector(("playerDidFinishPlaying:")), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: winMusic)
但是当歌曲播放完毕时,该函数没有被调用,我不知道为什么?这显然与 iOS 应用中的不同。
var winMusic = NSURL(fileURLWithPath: Bundle.main.path(forResource: "win", ofType: "mp3")!)
var winPlayer: AVAudioPlayer? = nil
class Responder : NSObject {
func playerDidFinishPlaying() {
print("test")
/* Nothing printed here */
}
func action(sender: UIButton) {
NotificationCenter.default.addObserver(self, selector: #selector(Responder.playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: winMusic)
NotificationCenter.default.post(name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
winPlayer!.play()
}
}
【问题讨论】:
-
好的,如何发布通知?
-
你能分享你的游乐场代码吗?
-
好的@DaveWeston,代码已发布。
-
好的,也发了。
标签: swift xcode swift3 avplayer swift-playground