Features

Developers

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

March 2, 2023 - Thibault Beyou in Android, Video upload, Kotlin

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:

	VideosApiStore.initialize("YOUR_API_KEY") // To upload with videoId
	// or VideosApiStore.initialize() // To upload with upload token

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.

NotificationConfigurationStore.notificationId = 1234 // A unique integer
NotificationConfigurationStore.channelId = "com.foo.bar.channel" // A unique string to identify the channel

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:

val operationWithRequest = WorkManager.getInstance(applicationContext)
	.upload("YOUR_VIDEO_ID", yourFilePath)

And to upload a file with an upload token:

val operationWithRequest = WorkManager.getInstance(applicationContext)
	.uploadWithUploadToken("YOUR_UPLOAD_TOKEN", yourFilePath)

👂 Listen to Worker events

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

WorkManager.getInstance(applicationContext)
 	// operationWithRequest is the returned parameter of `upload` API
	.getWorkInfoByIdLiveData(operationWithRequest.request.id)

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

WorkManager.getInstance(applicationContext)
	// operationWithRequest is the Vreturned parameter of `upload`
	.getWorkInfoByIdLiveData(operationWithRequest.request.id)
	 .observe(observer, Observer { workInfo: WorkInfo? ->
		if (workInfo != null) {
			when (workInfo.state) {
				WorkInfo.State.SUCCEEDED -> {
					// File has been successfully upload!
					// Get the video object with the extension
					val video = workInfo.outputData.toVideo()
				}
				else -> {
					// Do something
				}
            }
    })

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

WorkManager.getInstance(applicationContext)
	// operationWithRequest is the returned parameter of `upload`
	.getWorkInfoByIdLiveData(operationWithRequest.request.id)
	.observe(observer, Observer { workInfo: WorkInfo? ->
		if (workInfo != null) {
			// Get the progress of the file with the extension
			val progress = workInfo.progress.toProgress() 
			// Do something with progress information
		}
    })

🎨 Notifications customization

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

For example, you can set the notification icon:

NotificationConfigurationStore.notificationIconResourceId = R.drawable.ic_api_video_logo

Or the notification color:

NotificationConfigurationStore.notificationColorResourceId = R.color.primary_orange

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.

Thibault Beyou

Senior Mobile Developer

Create your free account

Start building with video now