Giter Club home page Giter Club logo

youtubekit's Introduction

YoutubeKit

YoutubeKit is a video player that fully supports Youtube IFrame API and YoutubeDataAPI for easily create a Youtube app.

Swift Cocoapods Carthage License

Important Referecens

YoutubeKit is created that based on the folowing references. If you are unsure whether it is a normal behavior or a bug, please check the following document first.

Example

This is an app using YoutubeKit. And simple video playback example is included into Example. You can create these functions very easy by using the YoutubeKit.

Example1 Example2
Feed Comment
Example3 Example4
Floating Rotate

What YoutubeKit?

YoutubeKit provides usefull functions to create Youtube app. it is consisting of the following two functions.

  • YTSwiftyPlayer (WKWebView + HTML5 + IFrame API)

  • YoutubeDataAPI

YTSwiftyPlayer

YTSwiftyPlayer is a video player that supports Youtube IFrame API.

Features:

  • High performance (Performance is 30% better than traditional UIWebView based player)
  • Low memory impact (maximum 70% off)
  • Type safe parameter interface(All IFrame API's parameters are supported as VideoEmbedParameter)

YoutubeDataAPI

This library supports YoutubeDataAPI (v3). For the details is Here.

Available API lists:

  • Actitivty(list)
  • Actitivty(insert)
  • Caption(list)
  • Channel(list)
  • ChannelSections(list)
  • Comment(list)
  • CommentThreads(list)
  • GuideCategories(list)
  • PlaylistItems(list)
  • Playlists(list)
  • Search(list)
  • Subscriptions(list)
  • VideoAbuseReportReasons(list)
  • VideoCategories(list)
  • Videos(list)

Get Started

Playback the youtube video.

import YoutubeKit

final class VideoPlayerController: UIViewController {

    private var player: YTSwiftyPlayer!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Create a new player
        player = YTSwiftyPlayer(
                    frame: CGRect(x: 0, y: 0, width: 640, height: 480),
                    playerVars: [.autoplay(true), .videoID("videoID-abcde")])

        // Set player view.
        view = player

        // Set delegate for detect callback information from the player.
        player.delegate = self
        
        // Load the video.
        player.loadPlayer()
    }
}

YTSwiftyPlayerDelegate

YTSwiftyPlayerDelegate is supports folowing delegate methods.

func playerReady(_ player: YTSwiftyPlayer)
func player(_ player: YTSwiftyPlayer, didUpdateCurrentTime currentTime: Double)
func player(_ player: YTSwiftyPlayer, didChangeState state: YTSwiftyPlayerState)
func player(_ player: YTSwiftyPlayer, didChangePlaybackRate playbackRate: Double)
func player(_ player: YTSwiftyPlayer, didReceiveError error: YTSwiftyPlayerError)
func player(_ player: YTSwiftyPlayer, didChangeQuality quality: YTSwiftyVideoQuality) 
func apiDidChange(_ player: YTSwiftyPlayer)    
func youtubeIframeAPIReady(_ player: YTSwiftyPlayer)    
func youtubeIframeAPIFailedToLoad(_ player: YTSwiftyPlayer)

Call IFrame API during playback.

// Pause the video.
player.pauseVideo()

// Seek after 15 seconds.
player.seek(to: 15, allowSeekAhead: true)

// Set a mute.
player.mute()

// Load another video.
player.loadVideo(videoID: "abcde")

Get video information using YoutubeDataAPI

First, Get API key from Here.

Next, Add this code in your AppDelegate.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // Set your API key here
    YoutubeKit.shared.setAPIKey("Your API key")

    return true
}

And then you can use YoutubeDataAPI request like this.

// Get youtube chart ranking
let request = VideoListRequest(part: [.id, .statistics], filter: .chart)

// Send a request.
ApiSession.shared.send(request) { result in
    switch result {
    case .success(let response):
        print(response)
    case .failed(let error):
        print(error)
    }
}

Example of response here.

VideoList(items: [YoutubeKit.Video(etag: "\"A8kisgyDEbllhHF9ooXPFFrkc/nR6_A9oyIoLTJuucY_UXeasjYNU\"",
kind: "youtube#video",
id: "jeiDjeJgF0",
contentDetails: nil,
statistics: Optional(YoutubeKit.Statistics.VideoList(dislikeCount: "1631", likeCount: "60307", commentCount: Optional("8675"), favoriteCount: "0", viewCount: "1259046")),
snippet: nil,
status: nil),
etag: "\"J67fSnfblalhHF0foXPiFFrkc/TZGPJdE22-LilSv4-3VNoPw1cS4\"",
kind: "youtube#videoListResponse",
pageInfo: YoutubeKit.PageInfo(resultsPerPage: 5, totalResults: 200))

Fetch the next page (Pagination)

var nextPageToken: String?
...

// Send some request
ApiSession.shared.send(request) { [weak self] result in
    switch result {
    case .success(let response):
    
        // Save nextPageToken
        self?.nextPage = response.nextPageToken
    case .failed(let error):
        print(error)
    }
}
...

// Set nextPageToken
let request = VideoListRequest(part: [.id], filter: .chart, nextPageToken: nextPageToken)

Authorization Request

If you want authorized request such as a getting your activity in Youtube, you set your access token before sending a request. To use GoogleSignIn, you can easily get your access token. pod 'GoogleSignIn'

First, Add this code in your AppDelegate.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // Set your access token for autheticate request
    YoutubeKit.shared.setAccessToken("Your access token")

    return true
}

And then you can use request requiring authorization, this is an example to get your Youtube activity.

// Get your Youtube activity
let request = ActivityListRequest(part: [.snippet], filter: .mine(true))

// Send a request.
ApiSession.shared.send(request) { result in
    switch result {
    case .success(let video):
        print(video)
    case .failed(let error):
        print(error)
    }
}

TODO

  • Support fully ACID YoutubeDataAPI requests.

Requirements

XCode 9+

Swift 4+

Installation

Cocoapods

Add this to your Podfile:

pod 'YoutubeKit'

and

$ pod install

Carthage

Add this to your Cartfile:

github "rinov/YoutubeKit"

and

$ carthage update

Author

Github: https://github.com/rinov

Twitter: https://twitter.com/rinov0321

Email: rinov[at]rinov.jp

License

YoutubeKit is available under the MIT license.

youtubekit's People

Contributors

rinov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.