我将视频插入到我的页面视图控制器的每个页面中。我在Xcode上使用了Page View Controller模板应用程序,因为我是一个菜鸟。前几个视频可以播放和播放内容,但经过几次扫描后,所有视频都变黑了。
我正在使用此Swift视频播放器库:https://github.com/piemonte/Player
任何帮助都会超赞!
模型控制器代码,为示例有意重复相同的值...
class ModelController: NSObject, UIPageViewControllerDataSource {
var pageTitleData: [String] = [
"Banana",
"Banana",
"Banana",
"Banana",
"Banana",
"Banana",
"Banana",
"Banana",
"Banana",
"Banana"
]
var videoURLData: [String] = [
NSBundle.mainBundle().pathForResource("banana", ofType:"mp4")!,
NSBundle.mainBundle().pathForResource("banana", ofType:"mp4")!,
NSBundle.mainBundle().pathForResource("banana", ofType:"mp4")!,
NSBundle.mainBundle().pathForResource("banana", ofType:"mp4")!,
NSBundle.mainBundle().pathForResource("banana", ofType:"mp4")!,
NSBundle.mainBundle().pathForResource("banana", ofType:"mp4")!,
NSBundle.mainBundle().pathForResource("banana", ofType:"mp4")!,
NSBundle.mainBundle().pathForResource("banana", ofType:"mp4")!,
NSBundle.mainBundle().pathForResource("banana", ofType:"mp4")!,
NSBundle.mainBundle().pathForResource("banana", ofType:"mp4")!
]
override init() {
super.init()
}
//
func viewControllerAtIndex(index: Int, storyboard: UIStoryboard) -> DataViewController? {
// Return the data view controller for the given index.
if (self.pageTitleData.count == 0) || (index >= self.pageTitleData.count) {
return nil
}
// Create a new view controller and pass suitable data.
let dataViewController = storyboard.instantiateViewControllerWithIdentifier("DataViewController") as! DataViewController
dataViewController.pageTitleDataObject = self.pageTitleData[index]
dataViewController.videoURLDataObject = self.videoURLData[index]
return dataViewController
}
//
func indexOfViewController(viewController: DataViewController) -> Int {
// Return the index of the given data view controller.
// For simplicity, this implementation uses a static array of model objects and the view controller stores the model object; you can therefore use the model object to identify the index.
return pageTitleData.indexOf(viewController.pageTitleDataObject) ?? NSNotFound
}
// MARK: - Page View Controller Data Source
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
var index = self.indexOfViewController(viewController as! DataViewController)
if (index == 0) || (index == NSNotFound) {
return nil
}
index--
return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
}
//
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
var index = self.indexOfViewController(viewController as! DataViewController)
if index == NSNotFound {
return nil
}
index++
if index == self.pageTitleData.count {
return nil
}
return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
}
}
DataViewController代码......
class DataViewController: UIViewController, PlayerDelegate {
@IBOutlet weak var veggieLabel: UILabel!
@IBOutlet weak var videoWrapper: UIView!
private var player: Player!
// video voodoo
convenience init() {
self.init(nibName: nil, bundle:nil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
var pageTitleDataObject: String = ""
var videoURLDataObject: String = ""
override func viewDidLoad() {
super.viewDidLoad()
self.view.autoresizingMask = ([UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight])
// tether player
self.player = Player()
// listen
self.player.delegate = self
// set size of player
self.player.view.frame = self.view.bounds
// add player
self.addChildViewController(self.player)
// add to videowrapper
self.videoWrapper.addSubview(self.player.view)
self.player.didMoveToParentViewController(self)
let fileUrl = NSURL.fileURLWithPath(videoURLDataObject)
self.player.setUrl(fileUrl)
// set video fill mode
self.player.fillMode = "AVLayerVideoGravityResizeAspectFill"
// set video playback to loop
self.player.playbackLoops = true
// add tap gesture to video player
let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGestureRecognizer:")
tapGestureRecognizer.numberOfTapsRequired = 1
self.player.view.addGestureRecognizer(tapGestureRecognizer)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.veggieLabel!.text = pageTitleDataObject
self.player.playFromBeginning()
}
// tap Gesture Handling
func handleTapGestureRecognizer(gestureRecognizer: UITapGestureRecognizer) {
switch (self.player.playbackState.rawValue) {
case PlaybackState.Stopped.rawValue:
self.player.playFromBeginning()
case PlaybackState.Paused.rawValue:
self.player.playFromCurrentTime()
case PlaybackState.Playing.rawValue:
self.player.pause()
case PlaybackState.Failed.rawValue:
self.player.pause()
default:
self.player.pause()
}
}
// player delegates
func playerReady(player: Player) { }
func playerPlaybackStateDidChange(player: Player) { }
func playerBufferingStateDidChange(player: Player) { }
func playerPlaybackWillStartFromBeginning(player: Player) { }
func playerPlaybackDidEnd(player: Player) { }
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}