YouTube player for SwiftUI

Overview

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.
You might also like...
📺 A news app using YouTube playlists, built with React Native
📺 A news app using YouTube playlists, built with React Native

NewsWatch-React-Native A YouTube News app for iOS, made with React-Native. Enjoy watching your daily news. Screencast: This project was bootstrapped w

MacTube is a webview wrapper for YouTube for Mac OS.
MacTube is a webview wrapper for YouTube for Mac OS.

MacTube MacTube is a webview wrapper for YouTube for Mac OS. It was made for peo

A YouTube copy for the Victor Roldan Dev channel
A YouTube copy for the Victor Roldan Dev channel

YouTubeClone La idea de la creación de este proyecto, es que tu tengas la oportunidad de ver como es el desarrollo completo de una aplicación, desede

Youtube-like double tap to forward/rewind animation with ripple effect.
Youtube-like double tap to forward/rewind animation with ripple effect.

VideoQuickSeeking Youtube-like double tap to forward/rewind animation with ripple effect. Please feel free to make pull requests. Example To run the e

Hide the "Latest Video Performance" tab in YouTube Studio

Hide Latest Video Performance - YT Studio Extension that hides the stupid YouTube Studio "Latest Video Performance" / "Latest YouTube Short Performanc

📽 A video player for SwiftUI, support for caching, preload and custom control view.
📽 A video player for SwiftUI, support for caching, preload and custom control view.

Features QuickStart Advances Installation Requirements License Demo Clone or download the project. In the terminal, run swift package resolve. Open Vi

Audio player demo based on Swift and SwiftUI, which can play local or network audio.
Audio player demo based on Swift and SwiftUI, which can play local or network audio.

SwiftAudioDemo Audio player demo based on Swift and SwiftUI, which can play local or network audio. In this demo, I have made a radio player to play n

An alternative to SwiftUI's VideoPlayer that includes an overlay button that will transition the player to be presented full screen.

The VideoPlayer provided by SwiftUI out of the box is great but it's missing one very important feature: full screen presentation mode. AZVideoPlayer behaves pretty much exactly like VideoPlayer, but adds the button that's provided by AVPlayerViewController to go full screen.

BMPlayer - A video player for iOS, based on AVPlayer, support the horizontal, vertical screen
BMPlayer - A video player for iOS, based on AVPlayer, support the horizontal, vertical screen

A video player for iOS, based on AVPlayer, support the horizontal, vertical screen. support adjust volume, brightness and seek by slide, support subtitles.

Comments
  • Fix config parameters & clean up the project

    Fix config parameters & clean up the project

    I noticed that the playerVars were always empty. So I fixed that, it now works.

    Also I did the following changes:

    • Create a model for the parameters instead of using a dictionary
    • move enums and structs out of the SwiftYouTubePlayer.swift file
    • make the config parameters all optional

    Have fun testing it all & merging it if you want to 🙂

    opened by resignedScientist 1
  • Error compiling because extra argument in call of WebViewConfig.

    Error compiling because extra argument in call of WebViewConfig.

    Hey,

    I found your project and used it, but unfortunately there was an error.

    I forked and fixed it, was only an extra call in the init. But I'm new to programming, and Git as well but I'd like to contribute.

    Can I create a pull request but is there a better way?

    opened by treboc 1
Owner
Gordan Glavaš
Gordan Glavaš
MMPlayerView - Custom AVPlayerLayer on view and transition player with good effect like youtube and facebook

MMPlayerView Demo-Swift List / Shrink / Transition / Landscape MMPlayerLayer ex. use when change player view frequently like tableView / collectionVie

Millman Yang 724 Nov 24, 2022
YouTube video player for iOS, tvOS and macOS

About XCDYouTubeKit is a YouTube video player for iOS, tvOS and macOS. Are you enjoying XCDYouTubeKit? You can say thank you with a tweet. I am also a

Cédric Luthi 2.9k Jan 7, 2023
WatchTube: a standalone WatchOS youtube player utilizing Download API for search data and video streaming

WatchTube is a standalone WatchOS youtube player utilizing Download API for sear

WatchTubeTeam 11 May 30, 2022
Musical Player - A Simple Musical Player For iOS

Musical_Player The app is a musical player. It was written as an task for a mobi

null 1 Nov 26, 2022
YouTubePlayerKit A Swift Package to easily play YouTube videos 📺

A Swift Package to easily play YouTube videos ??

Sven Tiigi 304 Jan 7, 2023
Lightweight YouTube Downloader for iOS

DownTube DownTube is a very lightweight app that allows you to download any YouTube video for offline use. Note: this app goes against YouTube's TOS a

Adam Boyd 158 Dec 10, 2022
Swift library for embedding and controlling YouTube videos in your iOS applications via WKWebView!

YouTubePlayer Embed and control YouTube videos in your iOS applications! Neato, right? Let's see how it works. 0.7.0 Update: WKWebView breaking change

Giles Van Gruisen 825 Jan 3, 2023
Loop videos on iOS and Android (assuming only one video like on YouTube)

Mobile Video Loop By: Andrew-Chen-Wang iOS Safari Extension (soon Android) that lets you loop a video on your current website. This only works for the

Andrew Chen Wang 0 Dec 19, 2021
The front-end of youtube iOS app developed using swift 5

Youtube-Front-end-iOS The front-end of youtube iOS app developed using swift 5 UIs 1)-The UI that corresponds to Home Tab bar "The first View " The fi

null 1 Nov 28, 2021
This project is a clone of YouTube. But the main intention is to show how to write clean code, using proper MVC patterns and re-usable coding methodologies!

YouTubeClone This project is a clone of YouTube. But the main intention is to show how to write clean code, using proper MVC patterns and re-usable co

Vamshi Krishna 169 Dec 10, 2022