简体   繁体   English

注册AVPlayer进行事件-如何知道AVPlayer播放电影已完成

[英]Register AVPlayer for events - how to know AVPlayer is done playing movie

I know that AVPlayerItem (a subclass of AVPlayer) is able to subscribe to the AVPlayerItemDidPlayToEndTimeNotification 我知道AVPlayerItem(AVPlayer的子类)能够订阅AVPlayerItemDidPlayToEndTimeNotification

AVPlayer does not have such capability, and I need to use AVPlayer unless there is no way around this and know when the playing of the (video in my case) is complete. AVPlayer没有这种功能,除非没有解决办法,否则我需要使用AVPlayer,并且不知道何时播放(本例中的视频)。

How do I do this in an AVPlayer? 如何在AVPlayer中做到这一点? I read some of the documentation for these classes and I do not see an immediate answer. 我阅读了这些类的一些文档,但没有立即得到答案。

-(void)setupVideoView{


    _videoPlayer = [AVPlayer playerWithURL:urlObjectHere];

    AVPlayerLayer *layer = [AVPlayerLayer layer];

    [layer setPlayer:_videoPlayer];
    [layer setFrame:_videoView.bounds];

    [_videoView.layer addSublayer:layer];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:_videoPlayer];




}

-(void)itemDidFinishPlaying:(NSNotification *) notification {
    // does not get called

    DLog(@"");



}

As I know, there's no way to know the complete time by AVPlayer layer. 据我所知,无法通过AVPlayer层了解完整时间。

AVAsset - AVPlayerItem - AVPlayer are three components combined AVPlayer in AVFoundation. AVAsset-AVPlayerItem-AVPlayer是AVFoundation中结合了AVPlayer的三个组件。 If you want DIY more functions of AVPlayer, you need to use lower level of AVPlayer, they are AVAsset and AVPlayerItem. 如果要DIY AVPlayer的更多功能,则需要使用较低级别的AVPlayer,它们是AVAsset和AVPlayerItem。

If you just need normal utilities of AVPlayer, just use AVPlayerViewController in AVKit I think. 如果您只需要AVPlayer的常规实用程序,请在我认为的AVKit中使用AVPlayerViewController。

It's not suggested to use MPMoviePlayer, most part of it has deprecated. 不建议使用MPMoviePlayer,其中大部分已被弃用。 Get used to AVPlayer... 习惯AVPlayer ...

I was able to solve the problem in the following way: I used an AVPlayerItem and created my AVPlayer from that item, subscribing the notifications to the player item. 我可以通过以下方式解决该问题:我使用了AVPlayerItem并从该项目创建了我的AVPlayer,并将通知订阅了播放器项目。

-(void)setupVideoView{


     AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:urlObjectHere];//edited

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];//edited

    _videoPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];//edited

    AVPlayerLayer *layer = [AVPlayerLayer layer];

    [layer setPlayer:_videoPlayer];
    [layer setFrame:_videoView.bounds];

    [_videoView.layer addSublayer:layer];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:_videoPlayer];




}

-(void)itemDidFinishPlaying:(NSNotification *) notification {
    // gets called finally


    AVPlayerItem *p = [notification object]; //bring video back to zero
    [p seekToTime:kCMTimeZero];

    DLog(@"");



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM