How to play a video with VideoPlayer in SwiftUI

VideoPlayer – is a view that displays content from a player and a native user interface to control playback.

Available iOS 14.0+, macOS 11.0+, Mac Catalyst 14.0+, tvOS 14.0+, watchOS 7.0+, Xcode 12.0+.

How to use the VideoPlayer view? Go to the co…


This content originally appeared on DEV Community and was authored by Anton Paliakou

VideoPlayer - is a view that displays content from a player and a native user interface to control playback.

Available iOS 14.0+, macOS 11.0+, Mac Catalyst 14.0+, tvOS 14.0+, watchOS 7.0+, Xcode 12.0+.

How to use the VideoPlayer view? Go to the code below:

import AVKit // 1
import SwiftUI

struct ContentView: View {

    // 2
    let videoUrl = URL(fileURLWithPath: Bundle.main.path(forResource: "PexelsSea", ofType: "mp4")!)

    var body: some View {
        VideoPlayer(player: AVPlayer(url: videoUrl)) // 3
            .frame(height: 320)
    }
}
  1. The first step is to import the AVKit framework.
  2. The second step is create video URL. The URL can be created for local video or remote. In our code case, a URL was created for local video from the bundle.
  3. Final step is to pass AVPlayer, as the first parameter to VideoPlayer view.

The VideoPlayer view has playback controls.

Result:

VideoPlayer
VideoPlayerLandscape

Video Overlay (Watermark)

A VideoPlayer has another init function with two parameters:

init(player: AVPlayer?,
     @ViewBuilder videoOverlay: () -> VideoOverlay
)
  1. First parameter is AVPlayer.
  2. Second parameter is videoOverlay closure. The closure returns a VideoOverlay view to present over the player’s video content. The important thing is this overlay view is fully interactive, but is placed below the system-provided playback controls, and only receives unhandled events.

Let's go to the code. As an example, will add a simple text in the lower right corner.

import AVKit 
import SwiftUI

struct ContentView: View {

    let videoUrl = URL(fileURLWithPath: Bundle.main.path(forResource: "PexelsSea", ofType: "mp4")!) 

    var body: some View {
         VideoPlayer(player: AVPlayer(url: url), videoOverlay: {
             VStack {
                 Spacer()
                 HStack {
                     Spacer()
                     Text("Code sample by ToniDevBlog")
                         .foregroundColor(.white)
                 }
             }.padding()
         }).frame(height: 320)
     }
}

Result:

VideoOverlay

Full source code available on GitHub

Article originally published at ToniDevBlog

Thanks for reading! See you soon. 👋


This content originally appeared on DEV Community and was authored by Anton Paliakou


Print Share Comment Cite Upload Translate Updates
APA

Anton Paliakou | Sciencx (2021-10-04T19:27:22+00:00) How to play a video with VideoPlayer in SwiftUI. Retrieved from https://www.scien.cx/2021/10/04/how-to-play-a-video-with-videoplayer-in-swiftui/

MLA
" » How to play a video with VideoPlayer in SwiftUI." Anton Paliakou | Sciencx - Monday October 4, 2021, https://www.scien.cx/2021/10/04/how-to-play-a-video-with-videoplayer-in-swiftui/
HARVARD
Anton Paliakou | Sciencx Monday October 4, 2021 » How to play a video with VideoPlayer in SwiftUI., viewed ,<https://www.scien.cx/2021/10/04/how-to-play-a-video-with-videoplayer-in-swiftui/>
VANCOUVER
Anton Paliakou | Sciencx - » How to play a video with VideoPlayer in SwiftUI. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/04/how-to-play-a-video-with-videoplayer-in-swiftui/
CHICAGO
" » How to play a video with VideoPlayer in SwiftUI." Anton Paliakou | Sciencx - Accessed . https://www.scien.cx/2021/10/04/how-to-play-a-video-with-videoplayer-in-swiftui/
IEEE
" » How to play a video with VideoPlayer in SwiftUI." Anton Paliakou | Sciencx [Online]. Available: https://www.scien.cx/2021/10/04/how-to-play-a-video-with-videoplayer-in-swiftui/. [Accessed: ]
rf:citation
» How to play a video with VideoPlayer in SwiftUI | Anton Paliakou | Sciencx | https://www.scien.cx/2021/10/04/how-to-play-a-video-with-videoplayer-in-swiftui/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.