I am using SWIFTUI for iOS 16 project, I have 2 view:
- ContentView (It has inside a regular NavigationStack with path binding)
- TeaserView (It a full screen video that I want it to autoplay the moment the view is called) Everything works as it supposed to, till I bind the path from the ContentView inside the Teaser view, at which point for unknown reason the autoplay stops working.
How to fix so to keep the autoplay?
Here is the ContentView:
import SwiftUI
struct ContentView: View {
@State private var path = NavigationPath()
var body: some View {
NavigationStack(path: $path) {
NavigationLink("Play Me", value: "")
.navigationDestination(for: String.self) {
txtValue in Teaser(path: $path)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
This is the TeaserView:
import SwiftUI
import AVKit
struct Teaser: View {
let videoURL = URL(string:"https://SomeURLwithIntroVideo.mp4")!
@Binding var path: NavigationPath
var body: some View {
let player = AVPlayer(url: videoURL)
ZStack {
VideoPlayer(player: player).scaledToFill().onAppear() {player.play()}
}
.navigationBarBackButtonHidden()
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button {
path.removeLast()
} label: {
Text("custom-btn")
}
}
}
}
}
struct Teaser_Previews: PreviewProvider {
static var previews: some View {
Teaser(path: .constant(NavigationPath()))
}
}
Any suggestion on how to fix?