Hi all, I am creating the app and I have hit a roadblock which I cannot seem to solve. Because my app needs to play different kinds of audio, either a radio stream or a ready-made MP3 file, I have decided to use AVPlayer for the task, since it can play the remote content. And so I have made a class wrapping the AVPlayer like so:
import Foundation
import AVKit
class AVPlayerObservable: ObservableObject {
@Published var player: AVPlayer? = nil
public init() {
}
}
I wrapped the class as I want to inject it into the environment. Never mind though, because after that I have written a view like so:
import AVKit
import Foundation
import SwiftUI
struct PodcastPlayerView: View {
@EnvironmentObject var player: AVPlayerObservable
init(url: URL) {
if player.player == nil {
player.player = AVPlayer(url: url)
}
else {
player.player?.pause()
player.player?.replaceCurrentItem(with: AVPlayerItem(url: url))
}
}
var body: some View {
Text("Test test test")
}
}
And finally I call this test view like so:
Button("Listen") {
PodcastPlayerView(url: URL(string: "https://tyflopodcast.net/pobierz.php?id=100&plik=0")!)
}
Of course, I also have injected my wrapper into the environment like so:
ContentView().environment(\.managedObjectContext, dataController.container.viewContext).environmentObject(api).environmentObject(player)
And finally to the interesting part, after tapping on the view XCode crashes with the following error:
SwiftUI/EnvironmentObject.swift:70: Fatal error: No ObservableObject of type AVPlayerObservable found. A View.environmentObject(_:) for AVPlayerObservable may be missing as an ancestor of this view.
(lldb)
What the check? Sorry if I am stupid but I am banging my head for a few hours now and i cannot find the solution, and my app is almost complete!!!