0

I'm developing an app for Swift and Sprite Kit (xCode 6.4, currently building for iOS 8.4). I'm using SKVideoNode in conjunction with AVPlayer to play a full-screen video. The code follows:

    let path = NSBundle.mainBundle().pathForResource("SPLASH_x", ofType:"mov")
    let vUrl = NSURL.fileURLWithPath(path!)

    let asset = AVAsset.assetWithURL(vUrl) as? AVAsset
    let playerItem = AVPlayerItem(asset: asset)
    let player = AVPlayer(URL: vUrl)
    SplashVideo = SKVideoNode(AVPlayer: player)
    SplashVideo!.xScale = self.size.width / SplashVideo!.size.width
    SplashVideo!.yScale = self.size.height / SplashVideo!.size.height
    SplashVideo!.position = CGPointMake(self.frame.midX, self.frame.midY)
    self.addChild(SplashVideo!)

    var observer: AnyObject? = nil
    observer = player.addPeriodicTimeObserverForInterval(CMTimeMake(1,30), queue: dispatch_get_main_queue(),
        usingBlock: { (time: CMTime) -> Void in
            let secs:Float64 = CMTimeGetSeconds(time)
            println("I think it's playing")
            if (secs > 0.01) {
                self.hideBackground()
                println("I think I'm done observing. Background hidden!")
                player.removeTimeObserver(observer!)
            }
    })
    println("I think I'm playing the splash video:")
    SplashVideo!.play()

(In case it's not clear, this happens in didMoveToView; I have imported Foundation, AVFoundation, and SpriteKit at the top of the file).

This works fine in the simulator; if I build and run for my iPad nothing happens at all--it displays a black screen, or if I remove the time observer (so that the background never gets hidden), I just see the background (The background is the first frame of the movie--I was experiencing a black flash at the beginning of video playback and am using the time observer as a masking technique to hide it). One of my users has reported that it worked for him until he upgraded to iOS9 (less of a concern), another reports that he hears the audio that goes with the .mov file but doesn't see the video itself (more of a concern). So I'm getting a variety of non-working behaviors, which is the best kind of bug. And by best I mean worst.

Things I have tried:

  1. Various versions and combinations of directly linking in Foundation, AVFoundation, SpriteKit when building.
  2. Using AVPlayerLayer instead of SpriteKit (no change in behavior for me, didn't deploy so I don't know if it would help any of my testers).
  3. Removing the time observer entirely (no change).
  4. Searching the interwebs (no help).
  5. Tearing my hair out (ouch).

All were ineffective. And now I am bald. And sad.

  • In ObjC I had a similar issue and it was because player would get released before it could play. You could try making your player a class var and seeing if that helps. I would like to think it isn't an issue in swift but maybe not. – Skyler Lauren Oct 10 '15 at 02:47

1 Answers1

0

Answering my own question: after much trial and error, it appears that you can't scale an SKVideoNode in iOS9 (or possibly this was never supported? The documentation is not clear). It's also true that the simulator for xCode 7 isn't playing video no matter what I do, which wasn't helping matters. In any case, what you can do is change the size property of the Node (and, I guess, let Sprite Kit do the scaling? Documentation seems spotty) and that seems to work:

let asset = AVAsset.assetWithURL(vUrl) as? AVAsset
let playerItem = AVPlayerItem(asset: asset)
let player = AVPlayer(URL: vUrl)
SplashVideo = SKVideoNode(AVPlayer: player)
SplashVideo!.size = self.size
SplashVideo!.position = CGPointMake(self.frame.midX, self.frame.midY)
self.addChild(SplashVideo!) 
  • I should add that it doesn't matter if you scale the same amount for both x and y (that is, I wasn't trying to make the video a different aspect ratio than its native one), or set it via the "setScale" method. No method of scaling an SKVideoNode seems to work in iOS9. Again, this might be purposeful or known, but I couldn't find any reference to it in the docs. – Paul Mariz Oct 21 '15 at 23:36