Skip to content

ChrisMash/AVPlayer-SwiftUI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AVPlayer-SwiftUI

This repository is the result of my 4-part blog on using AVPlayer in SwiftUI:

Disclaimer: I'm no SwiftUI pro, there are things about this code that don't feel right but it works. And that's something 😁. I'll try and improve it over time! This code is also not battle tested, but seems to be ok!

The app starts off with a menu to select between a video player or an audio player. Parts 1-3 refer to the video player. Part 4 refers to the audio player.

Parts 1-3: Video Player Controls

The following diagram shows the view structs/classes in VideoView.swift:

(multiple structs/classes in a single file? Yep, I think it's easier to learn from with all these small structs/classes all in the same file, but maybe don't structure your own code this way)

alt text

The annotations show one of the first things that isn't quite right. VideoPlayerUIView doesn't actually care about the videoPos, videoDuration or seeking values, only VideoPlayerControlsView does. But at the time of writing part 3 of the blog series it was the only way I'd found to make it work.

Part 4: Better Observation of the Player

As mentioned above one of the issues I had at the end of Part 3 was that I was observing the player's time and duration in VideoPlayerUIView, which didn't actually care about those values. Since then I've found a better way to observe the values elsewhere, and in fact if it's only audio playing then the VideoPlayerUIView isn't required at all, so for that reason it's presented as just an audio player.

Part 4 also allows switching what the player is playing, which was a bit of a problem in the prior parts of the blog.

The following diagram shows the view structs/classes in AudioView.swift:

alt text

Part 5: VideoPlayer is here!

With the unveiling of iOS 14 at WWDC20 we now have VideoPlayer available as a View in SwiftUI! So that almost does away with the need for parts 1-4 in this series! Look how much simpler it is:

alt text

VideoPlayer is SwiftUI's answer to AVPlayerViewController, so if you're happy with the default controls you can drop it right in and get easy video playback with minimal effort.

If, on the other hand, you want some custom UI then parts 1-4 of this blog series will give you some clues as to how to achieve that.

Releases

No releases published

Packages

No packages published

Languages