Search code examples
swiftuiavplayervideo-playeravkit

How can I hide the buttons on the Video Player with SwiftUI on macOS?


VideoPlayer(player: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "*****", ofType: "mp4")!)))

How can I hide the buttons on the VideoPlayer. I want the video to be repeated constantly. You can access the VideoPlayer object by importing the AVKit library.

import AVKit

enter image description here


Solution

  • To hide video controls on macOS (wrapping AVPlayerView):

    struct ContentView: View {
        let player = AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "IMG_0226", ofType: "mp4")!))
        var body: some View {
            AVPlayerControllerRepresented(player: player)
                .onAppear {
                    player.play()
                }
                .frame(width: 400, height: 400)
        }
    }
    
    struct AVPlayerControllerRepresented : NSViewRepresentable {
        var player : AVPlayer
        
        func makeNSView(context: Context) -> AVPlayerView {
            let view = AVPlayerView()
            view.controlsStyle = .none
            view.player = player
            return view
        }
        
        func updateNSView(_ nsView: AVPlayerView, context: Context) {
            
        }
    }
    

    To loop AVPlayer: How do you loop AVPlayer in Swift?