update: 2016/07/26
reference:
1. Swift Video Player using AVPlayerViewController
A. 新增專案:
1. iOS > Application > Single View Application > Next
2. > Next > Create
-----------------------------------------------------------------------------------------------
B. 設定 iOS 支援的最小版本:
專案 > General > Deployment Target: 8.0
C. 加入多媒體檔:
1. > 將多媒體檔案拖拉到專案資料夾內
> Finish
D. 將預設的 View Controller 嵌入到 Navigation Controller 裡:
1. > Main.storyboard > 點選右邊 View Controller
> Editor > Embed In > Navigation Controller
E. 新增給 ViewController 轉場(segue) 使用的 AV Player ViewController
1. 將 AVKit Player View Controller 拖拉到 Main.storyboard 裡.
> 拖拉到 AV Player ViewController 放開
> 選取 Manual Segue > Show
-----------------------------------------------------------------------------------------------
F. 幫 View Controller 的 Navigation Item 設定 Title 名稱:
> 在此為: Media Player
G. 新增二個 Button:
1. 在 View Controller 新增二個 Button: Play Local Video 與 Play Local Audio
> 水平置中
> 固定寬高, 保持寬高比, 固定與下邊界或上邊界的距離
> 結果:
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 繫結事件:
-----------------------------------------------------------------------------------------------
J. Main.storyboard 結果:
K. 執行程式:
> 打開 App
> 按下: Play Local Video
> 按下: Play Local Audio
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。