如何将视频添加到故事板页面 (Xcode 6)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28676408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add videos to a storyboard page (Xcode 6)
提问by user113085
I was woking on a small app project, and I made a video to put into the storyboard. My only problem, however, is that I don't know how to add the video to the storyboard. The ImageView doesn't accept the filetype (.mov). Online, I have only found a tutorial for Xcode 4, and nothing else. I need the video and not a GIF file because I don't want the GIF to loop forever. I'm using Swift.
我正在做一个小应用程序项目,我制作了一个视频以放入故事板。但是,我唯一的问题是我不知道如何将视频添加到故事板中。ImageView 不接受文件类型 (.mov)。在网上,我只找到了 Xcode 4 的教程,没有别的。我需要视频而不是 GIF 文件,因为我不希望 GIF 永远循环播放。我正在使用斯威夫特。
Thanks!
谢谢!
采纳答案by sharon devasia
I would recommend you to add the MediaPlayer.framework
under the build phase option
我建议您MediaPlayer.framework
在构建阶段选项下添加
Build phase -> link binary with library -> use the add button and type mediaplayer and add the MediaPlayer.framework
. Once done add the following code:
构建阶段 -> 将二进制文件与库链接 -> 使用添加按钮并输入 mediaplayer 并添加MediaPlayer.framework
. 完成后添加以下代码:
import UIKit
import MediaPlayer
class ViewController: UIViewController {
var moviePlayer: MPMoviePlayerController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let path = NSBundle.mainBundle().pathForResource("sample", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
self.moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = self.moviePlayer {
player.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
player.view.sizeToFit()
player.scalingMode = MPMovieScalingMode.Fill
player.fullscreen = true
player.controlStyle = MPMovieControlStyle.None
player.movieSourceType = MPMovieSourceType.File
player.repeatMode = MPMovieRepeatMode.One
player.play()
self.view.addSubview(player.view)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
回答by ChinaXiaoHong
I don't know if you want the video auto play or not. If you want an auto play video you can add a video using Mediaplayer. I past for you my code. I am just a beginner in swift, but it is working for me.
我不知道你是否想要视频自动播放。如果您想要自动播放视频,您可以使用 Mediaplayer 添加视频。我过去给你我的代码。我只是 swift 的初学者,但它对我有用。
First add the video to your project
首先将视频添加到您的项目中
and then in your code your can do like that:
然后在你的代码中你可以这样做:
import MediaPlayer
class YOURCLASS: UIViewController {
override func viewDidAppear(animated: Bool) {
playVideo()
}
var moviePlayer : MPMoviePlayerController?
func playVideo() {
let path = NSBundle.mainBundle().pathForResource("THENAMEOFYOURVIDEO", ofType:"m4v")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = CGRect(x: 0, y: 50, width: self.view.frame.size.width, height: 300)
player.prepareToPlay()
player.scalingMode = .AspectFill
player.controlStyle = .None
player.shouldAutoplay = true
player.repeatMode = MPMovieRepeatMode.One
self.view.addSubview(player.view)
}
}
}