3

I am following this tutorial that explain how to create an AVPlayer and use it with Swift UI. I am at the part where the seek bar becomes a progress bar for the video player.

So, following that part, I wrote the code as it's shown in the tutorial but at this following line of code,

player.addPeriodicTimeObserver(forInterval: CMTime(seconds: 0.5, preferredTimescale: 600), queue: nil) { time in
            guard let item = self.player.currentItem else{
                return
            }

            self.seekPos = time.seconds / item.duration.seconds
        }

the bracket just before the time in is underlined in red and I got the following error:

Escaping closure captures mutating 'self' parameter

Is there a way to fix this? I want that the slider becomes a video progress bar. The addPeriodicTimeObserver is situated in an init inside my struct.

EDIT: The tutorial I'm using is for iOS but I'm developing for macOS. I just made the changes so the code from that tutorial works for macOS.

Francis Dolbec
  • 175
  • 1
  • 11
  • Related to your answer, I suggest you read it : https://stackoverflow.com/a/41941810/5464805. I imagine that you are new to swift by the way you refers at trailing closures, so here is a bunch of subject you should learn about : closures, capture list, structs/class. – Olympiloutre Jul 04 '19 at 06:27
  • @Olympiloutre I saw that article, but I didn't really understood how to apply it to my problem and also, if it is applying to my case. – Francis Dolbec Jul 04 '19 at 06:32
  • you have an error "Escaping closure captures mutating 'self' parameter", I suppose you have a struct, then change it to a class to get rid of the error. I didnt try it personnaly but it worth the try. – Olympiloutre Jul 04 '19 at 06:36
  • 1
    just checked in a playground, you get this error because this is a `struct`. Change it to class to get rid of the error – Olympiloutre Jul 04 '19 at 06:42
  • Hmm... The thing is that my `struct` containing that code is called by another `struct`. If I change it into a class, would it f*** everything up right? – Francis Dolbec Jul 04 '19 at 06:46
  • probably. However it is your model that needs to be revised: why did you chose to use a struct at first? – Olympiloutre Jul 04 '19 at 23:25

1 Answers1

3

Try this

player.addPeriodicTimeObserver(forInterval: CMTime(seconds: 0.5, preferredTimescale: 600), queue: nil) {[self] time in
    guard let item = self.player.currentItem else{
        return
    }

    self.seekPos = time.seconds / item.duration.seconds
}

Hope to help you.

Thanh Vu
  • 1,599
  • 10
  • 14
  • It partially worked... It did compile and launch, but when I started to play the video, the app freeze and I got the following error: `Thread 1: Fatal error: Accessing State outside View.body` – Francis Dolbec Jul 04 '19 at 06:38
  • 1
    I think it is another problem in your code. The problem you post is only compile issue. – Thanh Vu Jul 04 '19 at 06:39
  • I followed a tutorial as I stated in my initial post so, I don't know how something else in my code could be wrong if it works for the tutorial's writer... – Francis Dolbec Jul 04 '19 at 07:03
  • When I try your solution, the error is highlighting this line of code `self.seekPos = time.seconds / item.duration.seconds`. It's the last line of code of my initial post. – Francis Dolbec Jul 04 '19 at 07:05
  • 1
    It's seems that I can't assign the value of the division to my variable `seekPos`. Cause when I just print the result of the division, everything is fine! – Francis Dolbec Jul 04 '19 at 10:07