0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Swift5で動画をアプリ内で再生させる方法

Last updated at Posted at 2021-03-20

パターン①

① 使いたい動画をプロジェクトに追加する。
*チェック’を忘れずに
スクリーンショット 2021-03-20 16.53.25.png

import AVKitをコントローラに追加

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
}

の中に

let player = AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "動画ファイル名", ofType: "動画ファイル拡張子名")!))
let layer = AVPlayerLayer(player: player)
layer.frame = view.bounds
view.layer.addSublayer(layer)
player.play()

を追加。

ビルドしてみると

スクリーンショット 2021-03-20 16.55.11.png

真ん中で再生される。

画面全体にしたい時 layer.videoGravity = .resizeAspectFill 追加
音を消したい時 player.volume = 0 追加

パターン②

①②はパターン①同様

 override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated) 

の中に

let player = AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "動画ファイル名", ofType: "動画ファイル拡張子名")!))
let vc = AVPlayerViewController()
vc.player = player
present(vc, animated: true)

を追加。

ビルドしてみると

スクリーンショット 2021-03-20 17.12.10.png

こんな感じで再生される。

参考にしたもの
https://www.youtube.com/watch?v=yqCJcdFl55I&t=258s

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?