Tutorials3 min read

Video API integrate player iOS swift

Switch your existing Swift player to api.video

Have you already developed your own player, but find it challenging to switch to the api.video player? We have a solution. In this tutorial, we will demonstrate how to play a video from api.video on your player.

Romain Petit

March 29, 2023

Have you already developed your own player, but find it challenging to switch to the api.video player?

We have a solution. In this tutorial, we will demonstrate how to play a video from api.video on your player.

Before starting

Before you start, make sure that you have already uploaded at least one video to api.video. If you haven't done so yet, you can follow this guide for assistance.

Extensions

In this tutorial, we will discuss extensions. If you are not familiar with extensions, I will explain them briefly, but if you are, feel free to skip ahead.

Extensions enable you to add custom functionality to a code without altering the original implementation.

For example, let's say you have a struct named "Person" with "firstname" and "lastname" as properties. You can create an extension to retrieve their full name.

swift

Adding the api.video Swift player library

In this section, we will cover the two main steps involved in adding the api.video Swift player to your project:

  1. Get the latest version by accessing it here.

  2. Add the ApiVideoPlayer package to your app using one of the following methods:

    a. SPM:

    • In the Project Navigator, select your project.
    • Click on the "Package Dependencies" tab in the Project section.
    • Click the "+" button at the bottom.
    • Paste the URL below into the search bar in the top right.
    • Finally, click the "Add package" button.

bash

b. Cocoapods:

  • Add the following to your Podfile.

ruby

  • Then run:

bash

  • Replace X.X.X with the latest version.

VideoOptions

Every video on api.video is assigned a unique identifier called the videoId, which can be found on the Dashboard or in the video list.

As you follow this tutorial, make sure to replace YOUR_VIDEO_ID with the actual videoId of your video.

swift

If you are using private videos, it will be necessary to provide the private token as well.

A private token is an exclusive token that guarantees access to a specific private video, but it can be used only once.

swift

AVPlayer Extension

The api.video Swift Player provides an extension for playing new videos with AVPlayer:

swift

If, for any reason, you encounter an error with the HLS stream, you can try again with an MP4 video.

To do this, you need to add an observer to your player and retry when the status fails:

swift

Congrats, you did it; you can now play your video from api.video 馃コ馃コ馃コ馃コ

(!) To get info on failed mp4, feel free to add an else statement after the if

Explore more

With the Swift Player client, you are not limited to just replacing the currentItem.

It offers the flexibility to construct a player by using either UIKit or SwiftUI.

To explore this feature further, I suggest checking out this article or the project on GitHub.

Try out more than 80 features for free

Access all the features for as long as you need.
No commitment or credit card required

Video API, simplified

Fully customizable API to manage everything video. From encoding to delivery, in minutes.

Built for Speed

The fastest video encoding platform. Serve your users globally with 140+ points of presence.聽

Let end-users upload videos

Finally, an API that allows your end-users to upload videos and start live streams in a few clicks.

Affordable

Volume discounts and usage-based pricing to ensure you don鈥檛 exceed your budget.