0

I have a view:

struct playerView : View {
@ObservedObject var viewModel = playerViewModel()
var body : some View {
   Vstack {
       Button( action: { viewModel.play() }) {
       Text("Play")
         }
       Text(viewModel.playing)
  }
}

and i have viewModel :

class playerViewModel: ObservableObject {
@Published var player = AVPlayer()
@Published var playing = false

func play() {
//some setup for player...
self.player.play()
}

But I have a problem, I already found some solutions, but i cant understand how to use it in my project. I want to View automatically refresh when player starts to play sound. The question is: Which way is better for implementing timeControlStatus checker in playerViewModel?

brain
  • 75
  • 6
  • 1
    If you just wanted to see when it started, you could just set your `playing` property to `true` in `play()`. But, I'm guessing you want more control than that. Here's a SO question about observing `AVPlayer`'s state: https://stackoverflow.com/questions/40781738/how-to-detect-avplayer-actually-started-to-play-in-swift – jnpdx May 28 '21 at 18:26
  • @jnpdx thx, so i guess its not possible without adding Observer directly to the player? – brain May 28 '21 at 18:45
  • 1
    It has to be observed somehow, via notifications, etc. What other type of solution were you hoping for? – jnpdx May 28 '21 at 19:01
  • i thought that class as ObservableObject enough) I have little experience with that. – brain May 28 '21 at 19:05
  • 1
    `ObservableObject` allows you to do things like set `@Published` properties, like you have, but you still have to have a circumstance where those values get set to something (like responding to a notification or callback). – jnpdx May 28 '21 at 19:07

0 Answers0