|
I’m trying to create a looping video player view. Based on Apple’s docs, I did this:
I instantiate it as part of a
There are problems with this approach:
I can’t think of a better way to manage this player state. Assigning state in |
|
For every item in your NavigationSplitView a new VideoPlayerView is created. The old one isn't reused so to speak. I don't know when the previous instances are killed from memory. There is some mechanic which reuses cells but I don't know how this works. This is done by the OS itself. What you can do (and IMHO should do) is to refactor your "Player" in an own class and use it as an environment object. This class should be responsible for playing and stopping the video. In this case you only have one instance of the player. You have to implement some logic for swapping the video though. Your VideoPlayerView has to use the newly created player class instead of the video directly. |
|
It's actually not creating a VideoPlayerView for every item. It creates one for every item in the selection. But the problem is that SwiftUI constantly re-creates the view (every time something changes), which means the constructor gets called, which means the suite of AVFoundation objects (AVPlayerItem, AVQueuePlayer, and AVPlayerLooper) get instantiated every time. I think I need to create a new struct containing those, and instantiate it in the @State declaration. I'll post again if that works. |
|
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.