Giter Club home page Giter Club logo

dailymotion-swift-player-sdk-ios's Introduction

Dailymotion Swift Player SDK for iOS

Build Status Version License Swift Platform

Requirements

  • Xcode 8 and later
  • Swift 4
  • iOS 8+

Note: this SDK is using IDFA collection (will be asked by Apple in iTunes Connect when you will submit your app in the store). You can still disable it if you really need to when instantiating DMPlayerViewController.

Note: If you require an Objective-C version of the library or support for iOS < 8, use the old version of the library.

Installation

The preferred way is via CocoaPods. To install, add the following to your Podfile:

use_frameworks!

pod 'DailymotionPlayerSDK`

Usage

In the view controller that is going to serve videos, keep a reference to the Dailymotion player, and set your class as delegate:

import UIKit
import DailymotionPlayerSDK

class VideoViewController: UIViewController {

  // The player container. See setupPlayerViewController()
  @IBOutlet private var containerView: UIView!

  private lazy var playerViewController: DMPlayerViewController = {
    // If you have an OAuth token, you can pass it to the player to hook up
    // a user's view history.
    let parameters: [String: Any] = [
      "fullscreen-action": "trigger_event", // Trigger an event when the users toggles full screen mode in the player
      "sharing-action": "trigger_event" // Trigger an event to share the video to e.g. show a UIActivityViewController
    ]
    let controller = DMPlayerViewController(parameters: parameters)
    controller.delegate = self
    return controller
  }()

  override func viewDidLoad() {
    super.viewDidLoad()
    setupPlayerViewController()
  }

  // Add the player to your view. e.g. add a container on your storyboard
  // and add the player's view as subview to that
  private func setupPlayerViewController() {
    addChildViewController(playerViewController)

    let view = playerViewController.view!
    containerView.addSubview(view)
    view.translatesAutoresizingMaskIntoConstraints = false
    // Make the player's view fit our container view
    NSLayoutConstraint.activate([
      view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
      view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
      view.topAnchor.constraint(equalTo: containerView.topAnchor),
      view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
    ])
  }

}

extension VideoViewController: DMPlayerViewControllerDelegate {

  // The delegate has 4 mandatory functions

  func player(_ player: DMPlayerViewController, didReceiveEvent event: PlayerEvent) {
    // Sends player events of either .namedEvent(name: String, data: [String: String]?) or .timeEvent(name: String, time: Double)
  }

  func player(_ player: DMPlayerViewController, openUrl url: URL) {
    // Sent when a user taps on an ad that can display more information
  }

  func playerDidInitialize(_ player: DMPlayerViewController) {
    // Sent when the player has finished initializing
  }

  func player(_ player: DMPlayerViewController, didFailToInitializeWithError error: Error) {
    // Sent when the player failed to initialized
  }

}

For a full list of events, see the player API events page

To playback a video, call the player's load method with the video's id:

func loadVideo(withId id: String) {
  playerViewController.load(videoId: id)
}

For a list of parameters, see the player API parameters page.

To handle events sent by the player, let's implement the event delegate method mentioned above:

func player(_ player: DMPlayerViewController, didReceiveEvent event: PlayerEvent) {
  switch event {
    case .namedEvent(let name, _) where name == "fullscreen_toggle_requested":
      toggleFullScreen()
    default:
      break
  }
}

private func toggleFullScreen() {
  // Keep track of the orientation via an isPlayerFullscreen bool
  isPlayerFullscreen = !isPlayerFullscreen
  updateDeviceOrientation()
  updatePlayerSize()
}

private func updateDeviceOrientation() {
  let orientation: UIDeviceOrientation = isPlayerFullscreen ? .landscapeLeft : .portrait
  UIDevice.current.setValue(orientation.rawValue, forKey: #keyPath(UIDevice.orientation))
}

private func updatePlayerSize() {
  if isPlayerFullscreen {
    playerHeightConstraint.constant = nextSize.height
  } else {
    // Keep track of the initial player's height, e.g. via a didSet handler in the constraint outlet
    playerHeightConstraint.constant = initialPlayerHeight
  }
}

See the Example directory for a working sample of all this in action.

License

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

dailymotion-swift-player-sdk-ios's People

Contributors

bitrise-dm avatar ekhoo avatar jangorman avatar jdatour avatar kocoai avatar remiguyon avatar romainbiard avatar sergeimikhan avatar vhosune avatar zouffin avatar

Stargazers

 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.