Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ChrisMash/AVPlayer-SwiftUI
Using AVPlayer in SwiftUI
https://github.com/ChrisMash/AVPlayer-SwiftUI
avplayer avplayer-swiftui ios swift swiftui video-player
Last synced: 4 days ago
JSON representation
Using AVPlayer in SwiftUI
- Host: GitHub
- URL: https://github.com/ChrisMash/AVPlayer-SwiftUI
- Owner: ChrisMash
- License: mit
- Created: 2019-09-11T09:48:40.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-21T17:39:53.000Z (almost 4 years ago)
- Last Synced: 2024-08-02T20:45:47.253Z (3 months ago)
- Topics: avplayer, avplayer-swiftui, ios, swift, swiftui, video-player
- Language: Swift
- Homepage:
- Size: 95.7 KB
- Stars: 248
- Watchers: 7
- Forks: 30
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AVPlayer-SwiftUI
This repository is the result of my 4-part blog on using AVPlayer in SwiftUI:
* [AVPlayer & SwiftUI](https://medium.com/@chris.mash/avplayer-swiftui-b87af6d0553)
* [AVPlayer & SwiftUI Part 2: Player Controls](https://medium.com/@chris.mash/avplayer-swiftui-part-2-player-controls-c28b721e7e27)
* [AVPlayer & SwiftUI Part 3: More Player Controls](https://medium.com/@chris.mash/avplayer-swiftui-part-3-more-player-controls-e64558875f8e)
* [AVPlayer & SwiftUI Part 4: Better Player Observing](https://medium.com/@chris.mash/avplayer-swiftui-part-4-better-player-observing-3e5c3f24419d)
* [AVPlayer & SwiftUI Part 5: VideoPlayer is here!]()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](https://github.com/ChrisMash/AVPlayer-SwiftUI/blob/master/img/uml-videoview.png "Structs/classes in VideoView.swift")
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](https://github.com/ChrisMash/AVPlayer-SwiftUI/blob/master/img/uml-audioview.png "Structs/classes in AudioView.swift")
## 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](https://github.com/ChrisMash/AVPlayer-SwiftUI/blob/master/img/uml-videoplayer.png "SwiftUI's own VideoPlayer View")
`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.