2

In the app, the user is supposed to watch a setup video. If they don't finish watching and click the 'close' button, I want to know where they stopped so I can have the video start at that spot once they start watching again.

I want to know what the time is that they stopped watching so I can save that number to the server for later use, using the following code:

player.seek(to: CMTimeMakeWithSeconds(637, 1))

I tried the following:

func showUnplayedVideo() {

    // 1. check to see if user has already watched the app overview video
    ref.child("videos")
        .child("appOverview")
        .child("watched")
        .observeSingleEvent(of: .value) { (snapshot) in

        let watched = snapshot.value as? Bool ?? false
        if !watched {

            // 2. show setup video popup on first load
            guard let videoURL = URL(string: VideoURL.appOverview.rawValue) else { print("url error"); return }
            let player = AVPlayer(url: videoURL)

            self.playerVC.player = player

            // 3. dismiss the player once the video is over and update Firebase
            NotificationCenter.default.addObserver(self,
                                                   selector: #selector(self.playerDidFinishPlaying),
                                                   name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                                                   object: self.playerVC.player?.currentItem)

            NotificationCenter.default.addObserver(self,
                                                   selector: #selector(self.playerWasStoppedPrematurely),
                                                   name: NSNotification.Name.AVPlayerItemFailedToPlayToEndTime,
                                                   object: self.playerVC.player?.currentItem)

            self.present(self.playerVC, animated: true) {
                self.playerVC.player?.play()
            }
        }
    }
}

@objc func playerWasStoppedPrematurely(note: NSNotification) {
    self.playerVC.dismiss(animated: true)
    print(playerVC.player?.currentTime())
}

The first notification 'AVPlayerItemDidPlayToEndTime' works great (for users who watch the video all the way through), but my other notification 'AVPlayerItemFailedToPayToEndTime' doesn't work (for users who stop part way through the video).

How do I get the stopping point of my users if they don't watch the whole video?

EDIT #1

This is what the screen looks like when the video is playing:

AVKit

4

1 回答 1

3

您正在从视图控制器呈现 playerVC。当 playerVC 关闭时,viewDidAppear()您的视图控制器将被调用。这是一个放置时间检查代码的选择。

原始答案

您已经在使用player.currentTime(). 为什么不把它放在关闭按钮处理程序中呢?像这样的东西

let current = Double(CMTimeGetSeconds(avPlayer.currentTime()))
于 2018-08-08T05:14:04.827 回答