From Remote URL
Download the video file#
Although you can pass a URL directly to the editor, we strongly recommend that you manage to download the remote resource yourself. This allows you more control over where and how the download task is being executed and avoids potential problems around UI locking.
Here, we download a video from a remote URL using coroutines and save it in a local file. While the download is running, we show a loader and dismiss it when the download is finished.
Start Editor#
Configure the LoadSettings
to set the source to the Uri
of the file. The VideoEditorBuilder
takes in the configured VideoEditorSettingsList
and starts the video editor. The result is obtained in the onActivityResult()
method when the editor exits.
class OpenVideoFromRemoteURL(private val activity: AppCompatActivity) : Example(activity) {// Although the editor supports adding assets with remote URLs, we highly recommend// that you manage the download of remote resources yourself, since this// gives you more control over the whole process.override fun invoke() {activity.lifecycleScope.launch {showLoader(true)val file = withContext(Dispatchers.IO) {runCatching {val file = File.createTempFile("imgly_video", ".mp4")URL("https://img.ly/static/example-assets/Skater.mp4").openStream().use { input ->file.outputStream().use { output ->input.copyTo(output)}}file}.getOrNull()}showLoader(false)file?.let {showEditor(Uri.fromFile(it))} ?: showMessage("Error downloading the file")}}override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {super.onActivityResult(requestCode, resultCode, intent)intent ?: returnif (requestCode == EDITOR_REQUEST_CODE) {val result = EditorSDKResult(intent)when (result.resultStatus) {EditorSDKResult.Status.CANCELED -> showMessage("Editor cancelled")EditorSDKResult.Status.EXPORT_DONE -> showMessage("Result saved at ${result.resultUri}")else -> {}}}}private fun showEditor(uri: Uri) {// In this example, we do not need access to the Uri(s) after the editor is closed// so we pass false in the constructorval settingsList = VideoEditorSettingsList(false)// Set the source as the Uri of the video to be loaded.configure<LoadSettings> {it.source = uri}// Start the video editor using VideoEditorBuilder// The result will be obtained in onActivityResult() corresponding to EDITOR_REQUEST_CODEVideoEditorBuilder(activity).setSettingsList(settingsList).startActivityForResult(activity, EDITOR_REQUEST_CODE)// Release the SettingsList once donesettingsList.release()}}