David Kachlon
David Kachlon

Reputation: 619

Generic parameter 'Label' could not be inferred SWIFTUI

Can someone tell me why on line 9 I get the error Generic parameter 'Label' could not be inferred

struct PlayerControlsView : View {
  @State var playerPaused = true
  @State var seekPos = 0.0
  let player: AVPlayer
  var body: some View {
    HStack {
      Button(action: {
        self.playerPaused.toggle()
        if self.playerPaused {
          self.player.pause()
        }
        else {
          self.player.play()
        }
      }) {
        Image(systemName: playerPaused ? "play" : "pause")
          .padding(.leading, CGFloat(20))
          .padding(.trailing, CGFloat(20))
      }
      Slider(value: $seekPos, from: 0, through: 1, onEditingChanged: { _ in
        guard let item = self.player.currentItem else {
          return
        }

        let targetTime = self.seekPos * item.duration.seconds
        self.player.seek(to: CMTime(seconds: targetTime, preferredTimescale: 600))
      })
        .padding(.trailing, CGFloat(20))
    }
  }
}

And of course how to fix it.

Upvotes: 0

Views: 785

Answers (1)

Rob Napier
Rob Napier

Reputation: 299345

Your Slider init is incorrect. There is no version that has from: and through:. in: [0...1] is the default, though, so you don't need it anyway.

  Slider(value: $seekPos, onEditingChanged: { _ in

SwiftUI error messages are generally useless. The way you find the error is to keep commenting out things until it compiles, and then add them back in.

However, what you're doing here isn't going to work. You can't put a AVPlayer inside a View. A View is a struct, and they get created, copied, and destroyed all the time. See How to stream remote audio in iOS 13? (SwiftUI).

Upvotes: 2

Related Questions