I do not understand why AVPlayer is always nil
Unexpectedly found nil while unwrapping an Optional value
struct SatelliteVideoView: View {
init() {
player = AVPlayer(url: URL(string: "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4")!)
}
var body: some View {
VideoPlayer(player: player!)
}
The documentation uses this code.
@Claude31 it seems the problem is with the instantation of AVPlayer inside init(). honeslty, i have no clue why it behaves this way. If i specify the url when the variable is declared, it works. I followed the sample here
https://www.swiftanytime.com/videoplayer-in-swiftui/
and it works if it is coded like this
@State var player = AVPlayer(url: URL(string: "https://swiftanytime-content.s3.ap-south-1.amazonaws.com/SwiftUI-Beginner/Video-Player/iMacAdvertisement.mp4")!)
but, if it is done like this, it does not work
init() {
if let url = URL(string: "https://swiftanytime-content.s3.ap-south-1.amazonaws.com/SwiftUI-Beginner/Video-Player/iMacAdvertisement.mp4") {
player = AVPlayer(url: url)
}
}
Thoughts?