我正在寻找一个示例或使用AVKIT从我的应用程序播放视频的源代码。我找不到使用新版Swift 2(IOS 9)的例子。由于MPOovieplayercontroller在IOS 9中已被弃用。任何帮助都将非常受欢迎。
答案 0 :(得分:8)
您可以使用iOS 8.0及更高版本中提供的AVPlayerViewController
课程:
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let moviePath = NSBundle.mainBundle().pathForResource("sample_iPod", ofType: "m4v")
if let path = moviePath {
let url = NSURL.fileURLWithPath(path)
let player = AVPlayer(URL: url)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
if let validPlayer = playerViewController.player {
validPlayer.play()
}
}
}
}
}
编辑:
如果没有额外的屏幕和自定义尺寸,您可以播放电影:
let url = NSURL.fileURLWithPath(path)
let player = AVPlayer(URL: url)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
playerViewController.view.frame = CGRectMake(20, 50, 300, 300)
self.view.addSubview(playerViewController.view)
self.addChildViewController(playerViewController)
player.play()