Coder Social home page Coder Social logo

pmattos / swift-youtube-player Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gilesvangruisen/swift-youtube-player

0.0 2.0 0.0 64 KB

Swift library for embedding and controlling YouTube videos in your iOS applications!

License: MIT License

Ruby 4.04% HTML 20.93% Swift 75.03%

swift-youtube-player's Introduction

YouTubePlayer

Embed and control YouTube videos in your iOS applications! Neato, right? Let's see how it works.

Installation

Carthage

Add this to your Cartfile:

github "gilesvangruisen/Swift-YouTube-Player"

…and then run carthage update

Don't forget to:

  • add YouTubePlayer.framework to the Link binary with libraries build phase
  • add YouTubePlayer.framework as an input file to the carthage copy-frameworks run script phase (only necesasry if you're building for iOS)

See Carthage for more information about using Carthage as a dependency manager.

Cocoapods

Ensure you are opting into using frameworks with use_frameworks!. Then add the following to your Podfile:

pod 'YouTubePlayer'

…and then run pod install.

Example

// Import Swift module
import YouTubePlayer

Build and lay out the view however you wish, whether in IB w/ an outlet or programmatically.

@IBOutlet var videoPlayer: YouTubePlayerView!
// init YouTubePlayerView w/ playerFrame rect (assume playerFrame declared)
var videoPlayer = YouTubePlayerView(frame: playerFrame)

Give the player a video to load, whether from ID or URL.

// Load video from YouTube ID
videoPlayer.loadVideoID("nfWlot6h_JM")
// Load video from YouTube URL
let myVideoURL = NSURL(string: "https://www.youtube.com/watch?v=wQg3bXrVLtg")
videoPlayer.loadVideoURL(myVideoURL!)

Controlling YouTubePlayerView

Each YouTubePlayerView has methods for controlling the player (play, pause, seek, change video, etc.) They are:

  • func loadVideoURL(videoURL: NSURL)
  • func loadVideoID(videoID: String)
  • func loadPlaylistID(playlistID: String)
  • func play()
  • func pause()
  • func stop()
  • func clear()
  • func seekTo(seconds: Float, seekAhead: Bool)
  • func previousVideo()
  • func nextVideo()

Please note that calls to all but the first two methods will result in a JavaScript runtime error if they are called before the player is ready. The player will not be ready until shortly after a call to either loadVideoURL(videoURL: NSURL) or loadVideoID(videoID: String). You can check the readiness of the player at any time by checking its ready: Bool property. These functions run asynchronously, so it is not guaranteed that a call to a play function will be safe if it immediately follows a call to a load function. I plan to update the library soon to add completion handlers to be called when the player is ready.

In the meantime, you can also the YouTubePlayerDelegate method playerReady(videoPlayer: YouTubePlayerView) to ensure code is executed immediately when the player becomes ready.

Responding to events

YouTube's iFrame player emits certain events based on the lifecycle of the player. The YouTubePlayerDelegate outlines these methods that get called during a player's lifecycle. They are:

  • func playerReady(videoPlayer: YouTubePlayerView)
  • func playerStateChanged(videoPlayer: YouTubePlayerView, playerState: YouTubePlayerState)
  • func playerQualityChanged(videoPlayer: YouTubePlayerView, playbackQuality: YouTubePlaybackQuality)

Side note: unfortunately, due to the way Swift protocols work, these are all required delegate methods. Setting a delegate on an instance of YouTubePlayer is optional, but any delegate must conform to YouTubePlayerDelegate and therefore must implement every one of the above methods. I wish there were a better way around this, but declaring the protocol as an @objc protocol means I wouldn't be able to use enum values as arguments, because Swift enums are incompatible with Objective-C enumerations. Feel free to file an issue if you know of some solution that lets us have optional delegate methods as well as the ability to pass Swift enums to these delegate methods.

swift-youtube-player's People

Contributors

gilesvangruisen avatar krzd avatar ymkjp avatar

Watchers

James Cloos avatar Paulo Mattos 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.