I encountered this problem too, I'm using Swift 5 and iOS 14.
What I noticed is that if you start the app in the view that has the VideoPlayer element, it works. But if you load a view that navigates or loads the VideoPlayer view, it won't work. In the example below, I've created a video that goes through all episodes and lists them out in a horizontal scroll.
When the user taps on one of the episodes, it'll open a popover with the video player in it, allowing the user to watch the video. the URL is dynamically generated too so it pulls the id from the episode from the ForEach.
Not sure if this is the best way; but hey it works 😁
swift
import SwiftUI
import AVKit
struct EpisodeList: View {
@State private var player: AVPlayer = AVPlayer(url: URL(string: "https://www.example.com/welcome.mp4")!)
@State private var isActive: Bool = false
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(episodes) { episode in
EpisodeItem(episode: episode)
.onTapGesture(perform: {
player = AVPlayer(url: URL(string: "https://www.example.com/episide_\(episode.id).mp4")!)
isActive = true
})
}
}
}
.popover(isPresented: $isActive, content: {
VideoPlayer(player: player)
.aspectRatio(contentMode: .fit)
.onAppear() {
player.play()
}
})
}
}
struct EpisodeList_Previews: PreviewProvider {
static var previews: some View {
EpisodeList()
}
}