1

I have an animated menu to start some videos. The video is implemented as one skVideoNode . When I press a menu button for the first time, the animation of the menu as well as the video start is delayed. I thought something have to load, so I preloaded all texture. The same, after some testing, it accurs at the place where the video is started. What could be the reason.

I initialize the avPlayer already at the program start, then I change only the item. the menu animated is triggered before but stops also for a seconds. why?

func setupVideo() {

    let fm = FileManager.default
    let urls = fm.urls(for: .documentDirectory, in: .userDomainMask)

    // if let url = urls.first as? URL {
    if let url = urls.first {
        let urlPath : String = url.path + "/" + "load.mp4"
        let item = AVPlayerItem(url:  URL(fileURLWithPath: urlPath) )
        let vPlayer = AVPlayer(playerItem: item)
        avPlayer = vPlayer
    }

}

trigged with the menu button

    if let url = urls.first {

        let urlPath : String = url.path + "/" + videoName
        let item = AVPlayerItem(url:  URL(fileURLWithPath: urlPath) )

        avPlayer.replaceCurrentItem(with: item)
        let timeInterval: CMTime = CMTimeMakeWithSeconds(0.5, 10)

        avPlayerTimeObserver = avPlayer.addPeriodicTimeObserver(forInterval: timeInterval,  queue: DispatchQueue.main) { (elapsedTime: CMTime) -> Void in
            let floatTime = Float(CMTimeGetSeconds(elapsedTime))
            // print("elapsedTime now:", CMTimeGetSeconds(elapsedTime) )
            self.avPlayerObserveTime(floatTime: floatTime)
        } as AnyObject
    }
bluelemonade
  • 1,115
  • 1
  • 14
  • 26
  • I remember from writing your last video node code that it took some time for the video to actually load... it looks like you may be re-loading the video each time here, which could be the problem (I'm not sure). Also, my last question you never accepted as answer, despite awarding bounty :) https://stackoverflow.com/a/44989379/6593818 – Fluidity Aug 28 '17 at 13:38
  • maybe try pre-loading all of the items as properties somewhere, then `.replaceCurrentItem` with that reference, instead of calling the URL. Just a guess. – Fluidity Aug 28 '17 at 13:40
  • oh, I thought I accepted it as answer. sorry! I'll do it now – bluelemonade Aug 29 '17 at 13:52
  • the problem is that every new video change is done wothout delay, only the first button down. – bluelemonade Aug 29 '17 at 13:53
  • ok, so the animation is delayed as well.. on first click.. – Fluidity Aug 29 '17 at 22:14
  • try disabling all the video stuff to see if it is the animation that is the issue – Fluidity Aug 29 '17 at 22:16
  • yes, when I disable the if let url = block, the animation starts immediately – bluelemonade Aug 30 '17 at 06:45
  • i still think you need a preloaded property fpr each video, not calling by url. it would take a few mins to make a struct and switch statement to try it out. – Fluidity Aug 30 '17 at 07:07
  • if you instantiate the struct at scene entry, i cant think of a reason it would lag – Fluidity Aug 30 '17 at 07:08
  • ok, you mean a struct with all items? – bluelemonade Aug 30 '17 at 11:47
  • yes. then a property for the struct instance in gamescene. then access via switch in your button – Fluidity Aug 30 '17 at 11:51

0 Answers0