因此,我已经为这个问题找到了大约16个小时的答案。这似乎是一件很简单的事情,但苹果公司关于这方面的文档非常糟糕,而且我对目标C的理解也不是很好。
所以我有一个SKSpriteNode,我想用它作为按钮。当这个按钮被点击时,我想在全屏上播放一个mp4视频。听起来很简单,对吧?
另外,我在游戏模板中写了这篇文章,因为我学到了我知道的那么快,而且我只在GameScene.swift工作。
我已经进入了构建阶段,并将视频添加到“复制捆绑资源”中。
class GameScene: SKScene {
var infoButton:SKSpriteNode!
var moviePlayer: MPMoviePlayerController!
var url: NSURL!
override func didMoveToView(view: SKView) {
// Button
let infoButtonTexture = SKTexture(imageNamed: "btn_vid")
infoButton = SKSpriteNode(texture: infoButtonTexture)
infoButton.position = CGPointMake(backgroundSprite.size.width * 0.393, backgroundSprite.size.height * 0.2437)
infoButton.userInteractionEnabled = false
infoButton.name = "infoButton"
self.addChild(infoButton)
// Video
let filePath = NSBundle.mainBundle().pathForResource("hue", ofType: "mp4")
let bundleURL = NSBundle.mainBundle()!.URLForResource("hue", withExtension: "txt")
url = NSURL(string: "hue.mp4")
moviePlayer.contentURL = NSURL.fileURLWithPath(filePath)
moviePlayer.fullscreen = true
moviePlayer.shouldAutoplay = true
moviePlayer.prepareToPlay()
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
println("Something has Been Touched")
var location:CGPoint = touch.locationInNode(self)
var node:SKNode!
node = self.nodeAtPoint(CGPoint(x: location.x, y: location.y))
if node.name {
if node.name == infoButton.name {
moviePlayer.play()
}
}
}
}我试着删除不相关的代码。
正如您在视频部分中注意到的,我有三种不同的方法来获取文件路径。我已经运行了它们的println,它们打印如下:
/Users/myname/Library/Developer/CoreSimulator/Devices/954CB013-FB55-4846-A8E0-A2773A3563FF/data/Containers/Bundle/Application/35DB47D4-FB78-4FBE-898B-A5B3435A0F6C/Kiosk-Wine.app/hue.mp4打印filePath
as:file:///Users/myname/Library/Developer/CoreSimulator/Devices/954CB013-FB55-4846-A8E0-A2773A3563FF/data/Containers/Bundle/Application/35DB47D4-FB78-4FBE-898B-A5B3435A0F6C/Kiosk-Wine.app/hue.mp4 bundleURL打印
url打印为: hue.mp4 (您可能已经知道了)
我对其中每一个都使用了moviePlayer.contentURL = NSURL.fileURLWithPath(x),其中x是filePath、bundleURL或url,错误如下:
filePath: EXC_BAD_INSTRUCTION:1 EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)在输出窗口中显示:致命错误:意外地发现为零,同时打开可选值(lldb)
bundleURL:此方法无法构建:调用中参数isDirectory缺少参数参数
url:这一次未能构建:调用中参数isDirectory的缺失参数
我希望我已经给你足够的信息,让你对这个问题有一个好的想法,如果没有,请让我知道,我会张贴它。
谢谢!
额外的问题是:如果有人知道识别一个“点击向下”和一个“发布”的代码,以便当有人触摸它时,我可以更改sprite节点的纹理图像,那就太棒了:)或者甚至知道我可以在哪里读到这方面的内容。
发布于 2014-08-08 23:24:43
试试这个:
let url = NSBundle.mainBundle().URLForResource("hue", withExtension: "mp4")
moviePlayer.contentURL = url
moviePlayer.fullscreen = true
moviePlayer.shouldAutoplay = true
moviePlayer.prepareToPlay()我没有太多的文件经验,但这是一个同事堆叠溢出用户在某篇文章中建议,当我在我的应用程序中寻找播放视频的方法。
https://stackoverflow.com/questions/25212201
复制相似问题