Tutorials · 3 min read

How to easily upload videos on your Android app with WorkManager

How to easily upload videos on your Android app with WorkManager?

The Android WorkManager component enables you to seamlessly upload files from your mobile app to api.video.

Thibault Beyou

March 2, 2023

This article presents a new way to upload files to api.video from an Android application. It is based on a specific Android component called: WorkManager. To simplify the usage for developers, we added dedicated methods to the Android API client and the Android API uploader that we are going to discover in this paper.

On a previous article, we demonstrated how to use an Android service: the UploadService. The WorkManager is an even easier and more powerful way to upload files. 🚀🚀👌👌

👷‍♀️👷‍♂️ Android WorkManager

The WorkManager is a component of the Android Jetpack. It is a library that makes it easy to schedule deferrable, asynchronous tasks that are expected to run even if the app exits or the device restarts. The tasks could be constraint to specific device state before it is launched. For example, the WorkManager will execute the task only when the network is available.

For these reasons, it is perfect to perform long running tasks on background such as uploads.

To simplify the usage, we have developed dedicated API available in our Android API client and in the Android API uploader.

👌 api.video for WorkManager

With the API provided by api.video with the WorkManager, you can:

  • upload with video id
  • upload with upload token
  • upload with progressive upload
  • display the progress notification of the upload of the current file
  • get the progress of the upload of the files
  • get the state of the upload of the files: success, error,…
  • multiple uploads: it uploads files from an internal queue
  • cancel the upload of a specific file or all files
  • upload only when your device is connected

🛠️ Implementation

Before uploading a video, you have to create a video client with the VideosApiStore:

kotlin

You also have to set notification Ids with the NotificationConfigurationStore. The NotificationConfigurationStore is a singleton store that contains configuration for the notification. We will have a look on the customization further.

kotlin

You can now call methods to upload your files. The easiest way is to use api.video WorkManager extensions.

To upload a file with a video Id in your Activity:

kotlin

And to upload a file with an upload token:

kotlin

👂 Listen to Worker events

To listen the upload worker, use the WorkInfo . Get it with:

kotlin

To listen to the upload status, use workInfo.state. You can also get the Video object with the toVideo() when the upload succeeds:

kotlin

To listen to the progress of your upload, use WorkInfo.progress with the toProgress() extension.

kotlin

🎨 Notifications customization

A light customization of the notifications could be performed with the NotificationConfigurationStore.

For example, you can set the notification icon:

kotlin

Or the notification color:

kotlin

To have more controls over the notifications (such as changing messages), you can extend both UploadWorker and ProgressiveUploadWorker and overwrite the onUploadStarted, onUploadError and onUploadProgressNotification.

With this article, you should be able to quickly add a state of the art upload to api.video feature in your application 💪

To start building right now with the WorkManager, check out this example and sign up for a free account.

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’t exceed your budget.