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:
- Various versions and combinations of directly linking in Foundation, AVFoundation, SpriteKit when building.
- 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).
- Removing the time observer entirely (no change).
- Searching the interwebs (no help).
- Tearing my hair out (ouch).
All were ineffective. And now I am bald. And sad.