I'm having the following issue:
Type 'AVPlayer.Type' cannot conform to 'ObservableObject'
struct MusicEditorView: View {
@ObservedObject var audioPlayer = AVPlayer
and this is the class:
class MusicPlayer: ObservableObject {
private var audioPlayer: AVPlayer?
private var timer: Timer?
func playSound(named sFileName: String){
if let url = Bundle.main.url(forResource: sFileName, withExtension: "mp3"){
audioPlayer = try? AVPlayer(url: url)
audioPlayer?.play()
}
}
func pause(){
audioPlayer?.pause()
}
func getcurrentProgress() -> Double{
guard let currentTime = audioPlayer?.currentItem?.currentTime().seconds else { return 0 }
guard let duration = audioPlayer?.currentItem?.duration.seconds else { return 0 }
return duration > 0 ? (currentTime / duration) * 100 : 0
}
func startProgressTimer(updateProgress: @escaping (Double, Double) -> Void){
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
guard let currentTime = self.audioPlayer?.currentItem?.currentTime().seconds else { return }
guard let duration = self.audioPlayer?.currentItem?.duration.seconds else { return }
updateProgress(currentTime, duration)
}
}
func stopProgressTimer(){
timer?.invalidate()
}
struct Sound: Identifiable, Codable {
var id = UUID()
var name: String
var fileName: String
}
}
}
I think the problem is this:
struct MusicEditorView: View {
@ObservedObject var audioPlayer = AVPlayer
^^^
You should be using a colon (:
) here, to indicate that you want to declare the audioPlayer
property to be of type AVPlayer
. The current code is declaring the property of a type type (a metatype), and that doesn’t make any sense in this context.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"