URL 동영상 재생 Swift3



구현



import UIKit
import AVFoundation


//クロージャーを多用 }()
class ViewController: UIViewController {


    //動画のURLを定義
    var url: URL = {
        var url = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
        return url!
    }()


     /*
     下記から動画画面の実装
     */

    //AVURLAssetは持ってないから
    //AVAssetResourceLoaderDelegateをしないといけないからextensionする
    lazy var asset: AVURLAsset = {
        var asset: AVURLAsset = AVURLAsset(url: self.url)
        asset.resourceLoader.setDelegate(self, queue: DispatchQueue.main)
        return asset
    }()


    lazy var playerItem: AVPlayerItem = {
        var playerItem: AVPlayerItem = AVPlayerItem(asset: self.asset)
        return playerItem
    }()


    lazy var player: AVPlayer = {
        var player: AVPlayer = AVPlayer(playerItem: self.playerItem)
        player.actionAtItemEnd = AVPlayerActionAtItemEnd.none
        return player
    }()

    //動画の画面
    lazy var playerLayer: AVPlayerLayer = {
        var playerLayer: AVPlayerLayer = AVPlayerLayer(player: self.player)
        playerLayer.frame = UIScreen.main.bounds
        playerLayer.backgroundColor = UIColor.clear.cgColor
        return playerLayer
    }()



    override func viewDidLoad() {
        super.viewDidLoad()

        view.layer.addSublayer(playerLayer)
        player.play()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension ViewController : AVAssetResourceLoaderDelegate {
}

출처



GitHub

URL 동영상 재생 Swift3

좋은 웹페이지 즐겨찾기