QuickAnswer
by

SwiftUI Video playback without displaying the video controller

SwiftUI Video playback without displaying the video controller

Play the video in Swift UI without displaying the video controller.
At the moment, when using VideoPlayer, the play button and controller are displayed over the video and cannot be erased.
Therefore, use AVPlayer.

Swift 5.3.2
Xcode 12.4
iOS Target 14.4
macOS 11.2.3 BigSur

Reference materials

https://medium.com/flawless-app-stories/avplayer-swiftui-b87af6d0553

https://medium.com/flawless-app-stories/avplayer-swiftui-part-2-player-controls-c28b721e7e27

Implementation example

Implemented as follows.
The explanation of the site that I referred to was for videos on the web, but the sample below is for playing a locally saved file.
The location of the video file is not in Assets.xcassets, but in ContentView.swift. (It may be different because I changed it on the way)

Example of playing a square video with full width

ContentView.swift

import SwiftUI
import AVKit

struct ContentView: View {
    private let player1: AVPlayer = AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "moviename", ofType: "mp4")!))

    var body: some View {
        GeometryReader { bodyView in
            VStack {
                PlayerView(player: self.player1)
            }.frame(width:bodyView.size.width, height:bodyView.size.width)
        }
    }

    private func movieStart() {
        self.player1.seek(to: CMTime(seconds: 0, preferredTimescale: 600))
        self.player1.play()
    }

}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct PlayerView: UIViewRepresentable {
    let player: AVPlayer

    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PlayerView>) {
    }

    func makeUIView(context: Context) -> UIView {
        return PlayerUIView(player: player)
    }

}

PlayerUIView.swift

import SwiftUI
import AVKit

class PlayerUIView: UIView {
    private let playerLayer = AVPlayerLayer()

    init(player: AVPlayer) {
        super.init(frame: .zero)
        playerLayer.player = player
        layer.addSublayer(playerLayer)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        playerLayer.frame = bounds
    }

}

VideoPlayer References

https://medium.com/flawless-app-stories/avplayer-swiftui-part-5-videoplayer-is-here-e7821a0fd4a9

CONTENTS
Web Browser