我在一个小应用项目上工作,我制作了一个视频放入故事板。然而,我唯一的问题是我不知道如何将视频添加到故事板。 ImageView不接受文件类型(.mov)。在线,我只找到了Xcode 4的教程,没有别的。我需要视频而不是GIF文件,因为我不希望GIF永远循环。我使用的是Swift。
谢谢!
答案 0 :(得分:2)
我不知道你是否想要视频自动播放。如果您想要自动播放视频,可以使用Mediaplayer添加视频。我过去为你的代码。我只是一个快速的初学者,但它对我有用。
首先将视频添加到您的项目中
然后在你的代码中你可以这样做:
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)
}
}
}
答案 1 :(得分:2)
我建议您在构建阶段选项
下添加MediaPlayer.framework
构建阶段 - >链接二进制文件库 - >使用添加按钮并键入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.
}
}