我创建了一个具有开始菜单屏幕的游戏。在这个菜单屏幕中,我想要一个视频(我通过iPhone 6模拟器录制我玩这个游戏)在这个菜单屏幕的背景中播放。我播放了视频,但是当视图加载时会显示菜单,然后它会显示视频替换菜单本身。
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
player?.play()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(MenuScene.loopVideo),
name: AVPlayerItemDidPlayToEndTimeNotification,
object: nil)
view.layer.addSublayer(playerLayer)
}
func loopVideo() {
player?.seekToTime(kCMTimeZero)
player?.play()
}
-------- -------- UPDATE
Here's the link to see what I'm talking about.带有文字的屏幕是我尝试播放视频的地方,但它只是让屏幕变白,然后播放视频本身。
我有任何问题或需要添加信息,只需评论。
谢谢,乔丹。
答案 0 :(得分:1)
尝试使用SKVideoNode
,而不是在视图中添加图层。
这样的事情:
let videoURL: NSURL = NSBundle.mainBundle().URLForResource("startTo32", withExtension: "mov")!
VideoSprite = SKVideoNode(url: videoURL)
self.addChild(VideoSprite)
VideoSprite.play()
SKVideoNode
文档可用here。
答案 1 :(得分:0)
这是您更新的Swift 4和Spritekit视频播放器:
import SpriteKit
import UIKit
import AVFoundation
class MenuScene: SKScene {
var player:AVPlayer!
var VideoSprite:SKVideoNode!
override func didMoveToView(view: SKView) {
let videoURL: NSURL = Bundle.main.url(forResource: "yourvideonamehere", withExtension: "mp4")! as NSURL
VideoSprite = SKVideoNode(url: videoURL as URL)
self.addChild(VideoSprite)
VideoSprite.play()
player = AVPlayer(url: videoURL as URL)
player?.actionAtItemEnd = .none
player?.isMuted = true
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
playerLayer.zPosition = 100
playerLayer.frame = view.frame
player?.play()
}
func loopVideo() {
player?.seek(to: CMTime.zero)
player?.play()
}
}
我希望这会有所帮助。