Swiftpack.co - globulus/swiftui-youtube-player as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
globulus/swiftui-youtube-player
YouTube player for SwiftUI
.package(url: "https://github.com/globulus/swiftui-youtube-player.git", from: "1.0.0")

SwiftUI YouTube Player for iOS and MacOS

Fully functional, SwiftUI-ready YouTube player for iOS 14+ and MacOS 11+. Actions and state are both delivered via SwiftUI @Bindings, meaking it dead-easy to integrate into any existing SwiftUI View.

Preview iOS

Installation

This component is distributed as a Swift package. Just add this repo's URL to XCode:

https://github.com/globulus/swiftui-youtube-player

How to use

  • Pass the config parameter to optionally set various player properties:
    • playInline
    • allowsControls
    • showInfo
  • The action binding is used to control the player - whichever action you want it to perform, just set the variable's value to it. Available actions:
    • idle - does nothing and can be used as the default value.
    • load(URLRequest) - loads the video from the provided URL.
    • loadID(String) - loads the video based on its YouTube ID.
    • loadPlaylistId(String) - loads a playlist based on its YouTube ID.
    • mute
    • unmute
    • play
    • pause
    • stop
    • clear
    • seek(Float, Bool - seeks the given position in the video.
    • duration - evaluates the video's duration and updates the state.
    • currentTime - evaluates the current play time and updates the state.
    • previous
    • next
  • The state binding reports back the current state of the player. Available data:
    • ready - true if the player is ready to play a video.
    • status - unstarted, ended, playing, paused, buffering, queued
    • quality - small, medium, large, hd720, hd1080, highResolution
    • duration - will be set after the duration action is invoked.
    • currentTime - will be set after the currentTime action is invoked.
    • error - set if an error ocurred while playing the video, nil otherwise.

Sample code

import SwiftUIYouTubePlayer

struct YouTubeTest: View {
    @State private var action = YouTubePlayerAction.idle
    @State private var state = YouTubePlayerState.empty
    
    private var buttonText: String {
        switch state.status {
        case .playing:
            return "Pause"
        case .unstarted,  .ended, .paused:
            return "Play"
        case .buffering, .queued:
            return "Wait"
        }
    }
    private var infoText: String {
        "Q: \(state.quality)"
    }
    
    var body: some View {
        VStack {
            HStack {
                Button("Load") {
                    action = .loadID("v1PBptSDIh8")
                }
                Button(buttonText) {
                    if state.status != .playing {
                        action = .play
                    } else {
                        action = .pause
                    }
                }
                Text(infoText)
                Button("Prev") {
                    action = .previous
                }
                Button("Next") {
                    action = .next
                }
            }
            YouTubePlayer(action: $action, state: $state)
            Spacer()
        }
    }
}

Recipe

For a more detailed description of the code, visit this recipe. Check out SwiftUIRecipes.com for more SwiftUI recipes!

Acknowledgements

Changelog

  • 1.0.0 - Initial release.

GitHub

link
Stars: 2
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics