How do you create a video splash screen without a control bar a Swift app

Asked on 2024-07-30

1 search

To create a video splash screen without a control bar in a Swift app, you can use the AVPlayerViewController and customize its appearance. Here are the steps you can follow:

  1. Initialize the AVPlayer and AVPlayerViewController:

    import AVKit
    import SwiftUI
    
    struct VideoSplashScreen: UIViewControllerRepresentable {
        let player: AVPlayer
    
        func makeUIViewController(context: Context) -> AVPlayerViewController {
            let playerViewController = AVPlayerViewController()
            playerViewController.player = player
            playerViewController.showsPlaybackControls = false // Hide control bar
            return playerViewController
        }
    
        func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
            // No update needed
        }
    }
    
  2. Embed the VideoSplashScreen in your SwiftUI view:

    struct ContentView: View {
        var body: some View {
            VideoSplashScreen(player: AVPlayer(url: URL(string: "your_video_url_here")!))
                .edgesIgnoringSafeArea(.all) // Make the video fullscreen
        }
    }
    
  3. Play the video automatically:

    struct VideoSplashScreen: UIViewControllerRepresentable {
        let player: AVPlayer
    
        func makeUIViewController(context: Context) -> AVPlayerViewController {
            let playerViewController = AVPlayerViewController()
            playerViewController.player = player
            playerViewController.showsPlaybackControls = false // Hide control bar
            player.play() // Start playing the video automatically
            return playerViewController
        }
    
        func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
            // No update needed
        }
    }
    

This approach ensures that the video plays without showing the control bar, providing a clean splash screen experience.

For more detailed information on customizing video playback in SwiftUI, you can refer to the session Build compelling spatial photo and video experiences from WWDC 2024.

How do you create a video splash screen without a control bar a Swift app | Ask WWDC