Enabling Support For Media Tweets and Video Playback
Welcome to the third part of TwitterClone tutorial series. This part focuses on enabling support for media tweets and video playback. In particular, you will learn how to upload images and videos to tweets using the Mux video API. In the previous tutorial, you discovered Adding Stream Feeds to the Timeline
Find the project demo and download the source code from GitHub.
What is Mux?
Mux provides developers with APIs that make it easy to build video into their products, handling everything from encoding, storage, and streaming, to the player experience Using Mux, developers can build live, on-demand, and real-time video experiences, and get important data about video performance. Using Mux, you can get insights into video engagement and track your hosted videos' performance using their data analytics.
Video is hard to build and even harder to scale and maintain. Mux has everything developers need to quickly launch a product with engaging video experiences. Read more here
- Publish quickly: Videos are encoded while the first viewers watch them, making them available to watch just a few seconds after upload.
- Engage users: Everything you need to build engaging experiences, from GIFs, thumbnails, storyboards, and captions to a beautiful player.
- Get data: Understand user engagement on your videos and improve the Quality of Experience for your viewers.
- Global scale and performance: Whether you're serving a few dozen streams or a few million, Mux manages it so you do not have to.
Create a Mux Account and Get Your Token
To integrate the Mux API with your app, you should generate an access token id and a secret key from the Mux's dashboard for authentication. You can grab these credentials by generating a new token from the video category on the dashboard. Check their excellent guide and all the steps you need to set up the SDK.
Integrating Mux’s Swift Upload SDK With Our TwitterClone App
The Swift Upload SDK allows developers to send and host video files on Mux's platform via an iOS app. The SDK supports large video file uploads by splitting them into different chunks. To integrate the SDK, we add Mux’s dependency as a Swift Package to our Tuist
We then add the dependency on the Tuist project, in
project.swift, to a target.
We now fetch the dependency by running:
And make sure to generate the Xcode project again by running:
Check the Implementation Details
The following Swift files in the TwitterClone app contain the implementation of the Mux upload SDK. Open
TwitterCloneAuth.swift under the folder Auth -> Sources to explore the networking implementation.
We wanted to provide the ability to capture the iOS device camera, record video, and upload it to the Mux servers. So, we created an implementation that allows the use of a Mux upload ID to post media assets to Mux and generate an asset ID. You can find the full video capturing and upload implementation on GitHub.
AddNewTweetView.swift from the Project Navigator under the folders TimelineUI -> Sources to explore the details of handling the video capturing.
Playing back an uploaded asset
If you upload a video asset to Mux, it gets an asset ID. We used the asset ID to fetch a playback URL for the uploaded asset. Check the full implementation in
Based on the asset ID, you get a playback ID, which creates the playback URL. To playback the video, we appended the playback ID to the playback URL. The result creates an M3U8 media file that allows HTTP Live Streaming (HLS).
This tutorial showed you how we integrated the Mux upload SDK with our TwitterClone project to get an HLS media playback. In the next tutorial series, you will learn about Searching and Following Users using Algolia.