我一直在尋找通過AVPlayerItem
和AVPlayer
文檔,並且似乎沒有任何回調項目完成播放時。我希望能有某種代表回撥,我們可以利用,或者AVPlayerActionAtItemEnd
會爲我們提供一個自定義操作。如何檢測AVPlayerItem何時完成播放?
我怎樣才能找出一種方法來檢測AVPlayer何時完成播放項目?
我一直在尋找通過AVPlayerItem
和AVPlayer
文檔,並且似乎沒有任何回調項目完成播放時。我希望能有某種代表回撥,我們可以利用,或者AVPlayerActionAtItemEnd
會爲我們提供一個自定義操作。如何檢測AVPlayerItem何時完成播放?
我怎樣才能找出一種方法來檢測AVPlayer何時完成播放項目?
它使用NSNotification
在播放完成時發出警報。完成後調用
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
方法:爲通知
註冊
-(void)itemDidFinishPlaying:(NSNotification *) notification {
// Will be called when AVPlayer finishes playing playerItem
}
斯威夫特-I-田間(第3版)
class MyVideoPlayingViewController: AVPlayerViewController {
override func viewDidLoad() {
// Do any additional setup after loading the view.
super.viewDidLoad()
let videoURL = URL(fileURLWithPath: Bundle.main.path(forResource: "MyVideo",
ofType: "mp4")!)
player = AVPlayer(url: videoURL)
NotificationCenter.default.addObserver(self,
selector: #selector(MyVideoPlayingViewController.animationDidFinish(_:)),
name: .AVPlayerItemDidPlayToEndTime,
object: player?.currentItem)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
player?.play()
}
func animationDidFinish(_ notification: NSNotification) {
print("Animation did finish")
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}
好答案,但一定要調用removeObserver(_:觀察員或在指定的addObserver任何對象之前的對象:):名稱選擇:名稱:對象:被釋放。 – pierre23
這是我如何做它。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:AVPlayerItemDidPlayToEndTimeNotification object:player.currentItem];
- (void)movieFinishedCallback:(NSNotification*)aNotification
{
// [self dismissViewControllerAnimated:YES completion:Nil];
}
你可以使用'[avPlayer CURRENTITEM]'如果你使用AVPlayerViewController – abhi1992