Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playback controls in swiftui

Tags:

ios

swift

swiftui

Attempting to use AVKit's AVPlayer to play a video without playback controls:

private let player = AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "videoToPlay", ofType: "mp4")!))
player.showsPlaybackControls = false

The above results in an error message that declarations must be separated by ';'. I've also tried:

VideoPlayer(player: player)
    .onAppear {
        player.showsPlaybackControls = false
    }

Which results in a different error.

Any advice on hiding playback controls with swiftui?

like image 383
devlo Avatar asked Jan 27 '21 21:01

devlo


Video Answer


2 Answers

VideoPlayer does not appear to have any methods on it that I can find in the documentation to control whether the playback controls are shown.

showPlaybackControls doesn't work because AVPlayer doesn't have that property either.

It looks like for now you'll have to do something like wrap an AVPlayerViewController:

Note that this is a very limited example and doesn't take into account a lot of scenarios you may need to consider, like what happens when AVPlayerControllerRepresented gets reloaded because it's parent changes -- will you need to use updateUIViewController to update its properties? You will also probably need a more stable solution to storing your AVPlayer than what I used, which will also get recreated any time the parent view changes. But, all of these are relatively easily solved architectural decisions (look into ObservableObject, StateObject, Equatable, etc)

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()
            }
    }
}

struct AVPlayerControllerRepresented : UIViewControllerRepresentable {
    var player : AVPlayer
    
    func makeUIViewController(context: Context) -> AVPlayerViewController {
        let controller = AVPlayerViewController()
        controller.player = player
        controller.showsPlaybackControls = false
        return controller
    }
    
    func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
        
    }
}
like image 141
jnpdx Avatar answered Oct 11 '22 01:10

jnpdx


I was having problems with playing segments with VideoPlayer, so I had to resort to using AVPlayerLayer which does not have the controls.

class PlayerView: UIView {

    // Override the property to make AVPlayerLayer the view's backing layer.
    override static var layerClass: AnyClass { AVPlayerLayer.self }
    
    // The associated player object.
    var player: AVPlayer? {
        get { playerLayer.player }
        set { playerLayer.player = newValue }
    }
    
    private var playerLayer: AVPlayerLayer { layer as! AVPlayerLayer }
}

struct CustomVideoPlayer: UIViewRepresentable {
    let player: AVQueuePlayer

    func makeUIView(context: Context) -> PlayerView {
        let view = PlayerView()
        view.player = player
        return view
    }
    
    func updateUIView(_ uiView: PlayerView, context: Context) { }
}

Usage:

CustomVideoPlayer(player: player)
                .onAppear() {
                    player.play()
                }
                .onDisappear() {
                    player.pause()
                }
                .ignoresSafeArea(.all)
like image 1
cora Avatar answered Oct 11 '22 00:10

cora