since: 2016/07/26
update: 2016/07/26reference:1. Swift Video Player using AVPlayerViewControllerA. 新增專案: 1.
iOS >
Application >
Single View Application >
Next
2. >
Next > Create
-----------------------------------------------------------------------------------------------B. 設定 iOS 支援的最小版本: 專案 >
General >
Deployment Target:
8.0
-----------------------------------------------------------------------------------------------C. 加入多媒體檔: 1. > 將
多媒體檔案拖拉到
專案資料夾內
2. > 記得
勾選 Copy items if needed >
Finish
-----------------------------------------------------------------------------------------------D. 將預設的 View Controller 嵌入到 Navigation Controller 裡: 1. >
Main.storyboard > 點選右邊
View Controller >
Editor >
Embed In >
Navigation Controller
2.
結果如下:
-----------------------------------------------------------------------------------------------E. 新增給 ViewController 轉場(segue) 使用的 AV Player ViewController 1. 將
AVKit Player View Controller 拖拉到
Main.storyboard 裡.
2. > 點選
ViewController > 按下
Ctrl +
滑鼠左鍵 > 拖拉到
AV Player ViewController 放開
> 選取
Manual Segue >
Show
3. 完成後, 右上方出現提示: 需要幫
Segue 設定
ID
4. 幫此
Segue 設定
ID :
seguePlayMedia
-----------------------------------------------------------------------------------------------F. 幫 View Controller 的 Navigation Item 設定 Title 名稱:
> 在此為: Media Player
-----------------------------------------------------------------------------------------------
G. 新增二個 Button: 1.
在 View Controller 新增二個 Button: Play Local Video 與 Play Local Audio
2. 新增
限制條件:
>
水平置中
> 固定寬高, 保持寬高比, 固定與下邊界或上邊界的距離
> 結果:
-----------------------------------------------------------------------------------------------H. 開啟 ViewController.swift 修改如下:import UIKit
//@add #######################
import AVKit
import AVFoundation
//class ViewController: UIViewController {
//@update #######################
class ViewController: UIViewController, AVPlayerViewControllerDelegate {
//@add #######################
// url for playing video
var mediaURL: NSURL!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//@add #######################
// action - play local video
@IBAction func playLocalVideo(sender: UIButton) {
// create video url form remote location (i.e. video stored at domain etc.)
//self.mediaURL = NSURL(string: "http://download.prashantmangukiya.com/SwiftVideoPlayer-Data/Big_Buck_Bunny_Trailer.m4v")
// create video url from local resource (i.e video stored within project folder)
self.mediaURL = NSBundle.mainBundle().URLForResource("everest", withExtension: "mov")!
// perform segue
self.performSegueWithIdentifier("seguePlayMedia", sender: self)
}
//@add #######################
// action - play Local Audio
@IBAction func playLocalAudio(sender: UIButton) {
// create audio url form local resource (i.e audio stored within project folder)
self.mediaURL = NSBundle.mainBundle().URLForResource("popular", withExtension: "mp3")!
// perform segue
self.performSegueWithIdentifier("seguePlayMedia", sender: self)
}
//@add #######################
// MARK: - Navigation function
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "seguePlayMedia" {
// get destination view controller
let destVc = segue.destinationViewController as! AVPlayerViewController
// set player
destVc.player = AVPlayer(URL: self.mediaURL)
//@add #######################
destVc.player?.play() // auto play
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}-----------------------------------------------------------------------------------------------I. 幫二個 Button 繫結事件:
1. "
Play Local Video" Button ->
playLocalVideo IBAction func
2. "
Play Local Audio" Button ->
playLocalAudio IBAction func
-----------------------------------------------------------------------------------------------J. Main.storyboard 結果:
-----------------------------------------------------------------------------------------------K. 執行程式:
>
打開 App
>
按下: Play Local Video
>
按下: Play Local Audio