簡體   English   中英

我怎么知道AVPlayer視頻何時結束?

[英]How do I know when an AVPlayer video has ended?

場景:iOS設備在進入AVPlayerViewController后自動通過AVPlayer播放視頻。

如何檢測AVPlayer何時完成?

您可以注冊以在播放器項目完成播放后接收AVPlayerItemDidPlayToEndTimeNotification通知。

NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:", name: AVPlayerItemDidPlayToEndTimeNotification, object: self.playerItem)

以防萬一它可以幫助某人。 我在Obj-C中知道它,但是我不得不將其添加到一些舊代碼中;)

- (void)setupVideo {

    NSString *stringVideoName = @"myVideo.mp4";
    NSString *stringVideoPath = [[NSBundle mainBundle] pathForResource:stringVideoName ofType:nil];

    NSURL *urlVideoFile = [NSURL fileURLWithPath:stringVideoPath];

    _playerViewController = [[AVPlayerViewController alloc] init];
    _playerViewController.player = [AVPlayer playerWithURL:urlVideoFile];
    _playerViewController.view.frame = self.videoView.bounds;
    _playerViewController.showsPlaybackControls = YES;

    // Identify when the player time position jumps.
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(playerItemTimeJumped:) name:AVPlayerItemTimeJumpedNotification object:_playerViewController.player.currentItem];

    [self.videoView addSubview:_playerViewController.view];
    self.videoView.autoresizesSubviews = YES;
}

- (void)playerItemTimeJumped:(NSNotification*)notification {
    AVPlayerItem *playerItem = notification.object;

    if (playerItem.status == AVPlayerItemStatusReadyToPlay) {
        [NSNotificationCenter.defaultCenter removeObserver:self name:AVPlayerItemTimeJumpedNotification object:notification.object];
// The video play button has been pressed only once. Tell someone about it.
        }
    }

迅捷4.0

 NotificationCenter.default.addObserver(self, selector: #selector(self.videoDidEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM