Skip to main content
Language:

Serialization

VideoEditor SDK supports serialization and deserialization, allowing your users to save and revise their work at any time. Serialization requires the backend:serializer module. Refer to our documentation on how to include the module in your project.

Start Editor#

For the sake of this example, we load a video from the resources into the video editor.

Configure the VideoEditorSaveSettings to set the outputMode to only export the SettingsList (instead of the video). This SettingsList will be used later for serialization. You can choose to skip this step and both the video as well as the SettingsList will be exported.

Handle Result#

The result from the editor is received in the onActivityResult() method. EditorSDKResult wraps around the intent and provides a convenient API to check the result. Here, we launch a coroutine using Dispatchers.IO. Inside the coroutine, we create a file, obtain the SettingsList (via EditorSDKResult.settingsList), and serialize it using the IMGLYFileWriter.writeJson() method. You can optionally serialize the SettingsList into a String (using IMGLYFileWriter.writeJsonAsString()) or a ByteArray (using IMGLYFileWriter.writeJsonAsBytes()).

For details on the JSON structure you can download our schema.

For further processing, you can upload the serialization to a remote server (compare Save Video To Remote URL) to make the editor state available on other devices as well.

The next section explains how to restore the editor state by deserializing these settings.

File:
class VideoSerialization(private val activity: AppCompatActivity) : Example(activity) {
override fun invoke() {
// In this example, we need access to the Uri(s) after the editor is closed
// so we pass true in the constructor
val settingsList = VideoEditorSettingsList(true)
// Set the source as the Uri of the video to be loaded
.configure<LoadSettings> {
it.source = activity.resourceUri(R.raw.skater)
}
// Export only SettingsList, we use this SettingsList later for serialization
// You can skip this if you want to export the video as well
.configure<VideoEditorSaveSettings> {
it.outputMode = OutputMode.EXPORT_ONLY_SETTINGS_LIST
}
// Start the video editor using VideoEditorBuilder
// The result will be obtained in onActivityResult() corresponding to EDITOR_REQUEST_CODE
VideoEditorBuilder(activity)
.setSettingsList(settingsList)
.startActivityForResult(activity, EDITOR_REQUEST_CODE)
// Release the SettingsList once done
settingsList.release()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
super.onActivityResult(requestCode, resultCode, intent)
intent ?: return
if (requestCode == EDITOR_REQUEST_CODE) {
val result = EditorSDKResult(intent)
when (result.resultStatus) {
EditorSDKResult.Status.CANCELED -> showMessage("Editor cancelled")
EditorSDKResult.Status.DONE_WITHOUT_EXPORT -> {
activity.lifecycleScope.launch(Dispatchers.IO) {
result.settingsList.use { settingsList ->
try {
val file = File(activity.getExternalFilesDir(null), "vesdk.json")
if (file.exists()) {
file.delete()
}
file.createNewFile()
// Serialize the settingsList as JSON into the file
IMGLYFileWriter(settingsList).writeJson(file)
withContext(Dispatchers.Main) {
showMessage("Serialisation saved successfully")
}
} catch (e: IOException) {
e.printStackTrace()
withContext(Dispatchers.Main) {
showMessage("Error saving serialisation")
}
}
}
}
}
else -> {
}
}
}
}
}