Code Monkey home page Code Monkey logo

youtubeplayerview's Introduction

YoutubePlayerView

CI Status Version License Platform

The YoutubePlayerView is an open source library that helps you embed a YouTube iframe player into an iOS application. The library creates a WKWebView and a bridge between your application’s Swift code and the YouTube player’s JavaScript code, thereby allowing the iOS application to control the YouTube player.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

Install the library via CocoaPods

YoutubePlayerView is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'YoutubePlayerView'

At the command line prompt, type pod install to update your workspace with the dependencies.

Tip: Remember that when using CocoaPods, you must open the .xcworkspace file in Xcode, not the .xcodeproj file.

Manually install the library

The library is also easy to install manually. Either download the source via GitHub’s download link or clone the repository. Once you have a local copy of the code, follow these steps:

  1. Open the sample project in Xcode or Finder.

  2. Select YoutubePlayerView.swift and YoutubePlayerExtras.swift. If you are opening the workspace in Xcode, these will be available under Pods -> Development Pods -> YoutubePlayerView. In the Finder, these are available in the project's root directory in the Classes directories.

  3. Drag these files and folders into your project. Make sure the Copy items into destination group’s folder option is checked.

Usage

Getting started to load video

To start playing youtube videos follow these steps:

  1. In interface builder drag a UIView to your scene.
  2. Select the Identity Inspector and change the class of the view to YoutubePlayerView.
  3. In interface builder create an IBOutlet of this view to yout view controller and name it playerView.
  4. Now in your view controller's viewDidLoad method add following code:
playerView.loadWithVideoId("GC5V67k0TAA")

Build and Run, after video loads tap on it to play the video.

Control video playback

You can also use loadWithVideoId(_ : with:) method to pass addition parameter to the view. For more information about the parameters visit Player Parameter

To play with additional parameter you can replace you code with this:

let playerVars: [String: Any] = [
    "controls": 1,
    "modestbranding": 1,
    "playsinline": 1,
    "rel": 0,
    "showinfo": 0,
    "autoplay": 1
]
playerView.loadWithVideoId("GC5V67k0TAA", with: playerVars)

There are also methods to control the playback:

func play()
func pause()
func stop()
func seek(to: allowSeekAhead:)

Handle player callbacks

The library provides a protocol YoutubePlayerViewDelegate to handle callbacks. Your class can conform to this protocol and set the delegate of the playerView to the class.

extension ViewController: YoutubePlayerViewDelegate {
    func playerViewDidBecomeReady(_ playerView: YoutubePlayerView) {
        print("Ready")
        playerView.play()
    }

    func playerView(_ playerView: YoutubePlayerView, didChangedToState state: YoutubePlayerState) {
        print("Changed to state: \(state)")
    }

    func playerView(_ playerView: YoutubePlayerView, didChangeToQuality quality: YoutubePlaybackQuality) {
        print("Changed to quality: \(quality)")
    }

    func playerView(_ playerView: YoutubePlayerView, receivedError error: Error) {
        print("Error: \(error)")
    }

    func playerView(_ playerView: YoutubePlayerView, didPlayTime time: Float) {
        print("Play time: \(time)")
    }
}

Now set the delegate of player view:

playerView.delegate = self

Author

Mukesh Yadav, [email protected]

License

YoutubePlayerView is available under the MIT license. See the LICENSE file for more info.

youtubeplayerview's People

Contributors

mukeshydv avatar rafarbr avatar

Watchers

James Cloos 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.