J Norris J Norris - 2 years ago 206
Swift Question

Video playing in background of SKScene

I created a game that has a starting menu screen. In this menu screen, I want a video(I recorded me playing this game via iPhone 6 simulator) to play in the background of this menu screen. I got the video to play, but what happens is when the view loads, it will show the menu, then it will show the video replacing the menu itself.

import SpriteKit
import UIKit
import AVFoundation

class MenuScene: SKScene {

var player:AVPlayer!
var VideoSprite:SKVideoNode!

override func didMoveToView(view: SKView) {
let videoURL: NSURL = NSBundle.mainBundle().URLForResource("startTo32", withExtension: "mov")!

player = AVPlayer(URL: videoURL)
player?.actionAtItemEnd = .None
player?.muted = true

let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
playerLayer.zPosition = -100

playerLayer.frame = view.frame


selector: #selector(MenuScene.loopVideo),
name: AVPlayerItemDidPlayToEndTimeNotification,
object: nil)



func loopVideo() {


Here's the link to see what I'm talking about. the screen with the words is where I'm trying to get the video to play, but instead it just makes the screen go white and then plays the video itself.

Any questions or need for me to add information, just comment.

Thanks, Jordan.

Answer Source

Try using your SKVideoNode instead of adding a layer to your view.

Something like this:

let videoURL: NSURL = NSBundle.mainBundle().URLForResource("startTo32", withExtension: "mov")!
VideoSprite = SKVideoNode(url: videoURL)

SKVideoNode documentation is available here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download