0
1

More than 3 years have passed since last update.

背景に動画を流す方法

Posted at

背景に動画を置く方法

画面の背景に動画をリピートで再生させ続けたい場合

viewController.swift

import UIKit
import AVFoundation

class PopupDetailViewController: UIViewController {

    var player = AVPlayer()
    let path = Bundle.main.path(forResource: "Sample", ofType: "mov")

    override func viewDidLoad() {
        super.viewDidLoad()
        player = AVPlayer(url: URL(fileURLWithPath: path!))
        player.play()

        let playerLayer = AVPlayerLayer(player: player)
//        フレームの大きさを決める
        playerLayer.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)
        playerLayer.videoGravity = .resizeAspectFill

        playerLayer.repeatCount = 0
        playerLayer.zPosition = -1
        view.layer.insertSublayer(playerLayer, at: 0)

//        リピートさせる
        NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) { (notification) in
            self.player.seek(to: .zero)
            self.player.play()
        }
    }

動画の保存場所

スクリーンショット 2020-09-06 9.21.47.png

ここにファイルを移せば再生される

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1