Skip to content

Latest commit

 

History

History
286 lines (209 loc) · 9.31 KB

README.md

File metadata and controls

286 lines (209 loc) · 9.31 KB

badge   badge   badge

api.video Swift player

api.video is the video infrastructure for product builders. Lightning fast video APIs for integrating, scaling, and managing on-demand & low latency live streaming features in your app.

Table of contents

Project description

Easily integrate a video player for videos from api.video in your Swift application.

Getting started

Installation

Swift Package Manager

In the Project Navigator select your own project. Then select the project in the Project section and click on the Package Dependencies tab. Click on the "+" button at the bottom. Paste the below url on the search bar on the top right. Finaly click on "Add package" button.

 https://github.com/apivideo/api.video-swift-player

Or add this in your Package.swift

  dependencies: [
        .package(url: "https://github.com/apivideo/api.video-swift-player.git", from: "1.3.0"),
    ],

Cocoapods

Add pod 'ApiVideoPlayer', '1.3.0' in your Podfile

Run pod install

Retrieve your video Id

At this point, you must have uploaded a least one video to your account. If you haven't see how to upload a video. You'll need a video Id to use this component and play a video from api.video. To get yours, follow these steps:

  1. Log into your account or create one here.
  2. Copy your API key (sandbox or production if you are subscribed to one of our plan).
  3. Go to the official api.video documentation.
  4. Go to API Reference -> Videos -> List all videos
  5. Create a get request to the /videos endpoint based on the reference, using a tool like Postman.
  6. Copy the "videoId" value of one of elements of the API response.

Alternatively, you can find your video Id in the video details of your dashboard.

Usage

  1. Imports the library
import ApiVideoPlayer
  1. Instantiates the player view
let playerView: ApiVideoPlayerView = ApiVideoPlayerView(frame: .zero, videoOptions: VideoOptions(videoId: "YOUR_VIDEO_ID", videoType: .vod)) // for private video VideoOptions(videoId: "YOUR_VIDEO_ID", videoType: .vod, token: "YOUR_PRIVATE_VIDEO_TOKEN")
  1. Adds the player view as a subview of your view controller
override func viewDidLoad() {
    ...
    self.addSubview(playerView)
}
  1. Delegates the player events

To be able to use the player delegate, you must implement the PlayerDelegate protocol.

extension YourViewController: PlayerDelegate {
    public func didPrepare() {
        // Do what you want when didPrepare is called
    }

    public func didReady() {
        // Do what you want when didReady is called
    }

    public func didPause() {
        // Do what you want when didPause is called
    }

    public func didPlay() {
        // Do what you want when didPlay is called
    }

    public func didReplay() {
        // Do what you want when didReplay is called
    }

    public func didMute() {
        // Do what you want when didMute is called
    }

    public func didUnMute() {
        // Do what you want when didUnMute is called
    }

    public func didLoop() {
        // Do what you want when didLoop is called
    }

    public func didSetVolume(_: Float) {
        // Do what you want when didSetVolume is called
    }

    public func didSeek(_: CMTime, _: CMTime) {
        // Do what you want when didSeek is called
    }

    public func didEnd() {
        // Do what you want when didEnd is called
    }

    public func didError(_: Error) {
        // Do what you want when didError is called
    }

    public func didVideoSizeChanged(_: CGSize) {
        // Do what you want when didVideoSizeChanged is called
    }
}
  1. Registers the delegate
override func viewDidLoad() {
    ...
    self.playerView.addDelegate(self)
}
  1. To use fullscreen, you must pass the view controller to the player view
override func viewDidAppear(_ animated: Bool) {
    ...
    playerView.viewController = self
}

Remote control

If you want to enable the remote control do the following:

override func viewDidLoad() {
    ...
    self.playerView.enableRemoteControl = true
}

When you have to remove it set enableRemoteControl to false

By default the remote control is hidden.

Supported player views

The api.video Swift player comes with a specific view ApiVideoPlayerView to display the video and its controller. If you require a customization of this view such as changing a button color,..., you can contact us.

Otherwise, in the ApiVideoPlayerController, you can also use the following views:

// Create the api.video controller
let controller = ApiVideoPlayerController(
    videoOptions: VideoOptions(videoId: "vi77Dgk0F8eLwaFOtC5870yn", videoType: .vod),
    delegates: [],
    autoplay: false
)
// Create the AVKit AVPlayerViewController
let viewController = AVPlayerViewController()

/// Pass the api.video controller to the AVKit AVPlayerViewController
viewController.setApiVideoPlayerController(controller)
// Prepare the view
self.addChild(viewController)
view.addSubview(viewController.view)
// Set the AVKit AVPlayerViewController frame size according to your needs (here it's the whole screen)
viewController.view.frame = self.view.frame
// Do what you want with the video controller (play, pause, seek,...)
controller.play()
  • AVPlayerLayer. A view that only display the video. It requires more work to be used.

Play an api.video video in your AVPlayer

If you are using AVPlayer directly, you can use the api.video Swift extensions:

  1. Create a video
let videoOptions = VideoOptions(videoId: "YOUR_VIDEO_ID", videoType: .vod))
// for private video VideoOptions(videoId: "YOUR_VIDEO_ID", videoType: .vod, token: "YOUR_PRIVATE_VIDEO_TOKEN")
  1. Pass it to your AVPlayer
val player = AVPlayer() // You already have that in your code
avPlayer.replaceCurrentItem(withHls: videoOptions)

Sample application

A demo application demonstrates how to use player. See /example folder.

Documentation

Dependencies

We are using external library

Plugin README
ApiVideoPlayerAnalytics README.md

FAQ

If you have any questions, ask us here: https://community.api.video or use Issues.