#65 AVPlayerViewController 搭配 IBSegueAction 播影片

Timmy-Luo
Aug 16, 2021

--

在 project navigator 加入影片檔

從 storyboard 設計畫面

加入 AVPlayerViewController (AVKit Player View Controller)

建立一個 Button 並從 Button 連到 AVPlayerViewController

建立 IBSegueAction function

將 Segue 按著右鍵拖到 ViewController

會自動出現這樣

⚠️加入函式庫

import AVKit

完整程式碼

匯入檔案的寫法👇

@IBSegueAction func showVideo(_ coder: NSCoder) -> AVPlayerViewController? {    let controller = AVPlayerViewController(coder: coder)    let url = Bundle.main.url(forResource: "雪の華", withExtension:
"mp4")
controller?.player = AVPlayer(url: url!) controller?.player?.play() return controller}

貼網址的寫法👇

@IBSegueAction func showVideo(_ coder: NSCoder) -> AVPlayerViewController? {    let controller = AVPlayerViewController(coder: coder)    let url = URL(string: "https://bit.ly/3jSw2wS")    controller?.player = AVPlayer(url: url!)    controller?.player?.play()    return controller}

參考文章

--

--