はじめに
SwiftUIで動画を再生するサンプルを記事にしときます。
サンプルアプリ
実装
インターネット上にある動画を再生する
import SwiftUI
import AVKit
struct ContentView: View {
let url: URL = URL(string: "https://taishin-miyamoto.com/sample.mp4")!
var player: AVPlayer? {
.init(url: url)
}
var body: some View {
VideoPlayer(player: player)
}
}
ローカルに用意した動画を再生する
import SwiftUI
import AVKit
struct ContentView: View {
let url: URL = Bundle.main.url(forResource: "sample", withExtension: "mp4")!
var player: AVPlayer {
.init(url: url)
}
var body: some View {
VideoPlayer(player: player)
}
}
おわり
UIKitをラップして使わないといけない記憶があったのですが、めっちゃ簡単に使えました