# Capturing Videos

## OzCapsula (SDK v8.22 and newer)

{% hint style="warning" %}
Please note: all required data (other than the video) must be packaged into the container before starting the Liveness screen.
{% endhint %}

To start recording, use `startActivityForResult`:

{% code title="Kotlin" %}

```kotlin
val sessionToken: String = getSessionToken()
val captureRequest = CaptureRequest(
    listOf(
        AnalysisProfile(
            Analysis.Type.QUALITY,
            listOf(MediaRequest.ActionMedia(OzAction.Blank)),
        )
    )
)
val intent = OzLivenessSDK.createStartIntent(captureRequest, sessionToken)
startActivityForResult(intent, REQUEST_LIVENESS_CONTAINER)
```

{% endcode %}

To obtain the captured video, use `onActivityResult`:

{% code title="Kotlin" %}

```kotlin
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == REQUEST_LIVENESS_CONTAINER) {
        when (resultCode) {
            OzLivenessResultCode.SUCCESS -> runAnalysis(OzLivenessSDK.getContainerFromIntent(data))
            OzLivenessResultCode.USER_CLOSED_LIVENESS -> { /* user closed the screen */ }
            else -> {
                val errorMessage = OzLivenessSDK.getErrorFromIntent(data)
                /* show error */
            }
        }
    }
}
```

{% endcode %}

If you use fragment, please refer to the example below. `LivenessFragment` is the [Fragment](https://developer.android.com/guide/fragments) representation of the Liveness screen UI.&#x20;

```kotlin
childFragmentManager.beginTransaction()
    .replace(android.R.id.content, LivenessFragment.create(createCaptureRequest(), sessionToken))
    .commit()

childFragmentManager.setFragmentResultListener(OzLivenessSDK.Extra.REQUEST_CODE, this) { _, result ->
    when (val resultCode = result.getInt(OzLivenessSDK.Extra.EXTRA_RESULT_CODE)) {
        OzLivenessResultCode.SUCCESS -> {
            val container = result.getParcelable(OzLivenessSDK.Extra.EXTRA_DATA_CONTAINER) as? DataContainer
            /* Run analysis */
        }
        OzLivenessResultCode.USER_CLOSED_LIVENESS -> { /* User closed the screen */ } 
        else -> { /* Show error */ }
    }
}
```

## SDK 8.21 and older

To start recording, use the`startActivityForResult` method:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val intent = OzLivenessSDK.createStartIntent(listOf(OzAction.Smile, OzAction.Blank))
startActivityForResult(intent, REQUEST_CODE)
```

{% endtab %}

{% tab title="Java" %}

```java
List<OzAction> actions  = Arrays.asList(OzAction.Smile, OzAction.Scan);
Intent intent = OzLivenessSDK.createStartIntent(actions);
startActivityForResult(intent, REQUEST_CODE);
```

{% endtab %}
{% endtabs %}

`actions` – a list of [user actions](https://doc.ozforensics.com/oz-knowledge/guides/developer-guide/sdk/oz-mobile-sdk/android-sdk-methods-and-properties#h.2nyke2meu4aw) while recording video.

For Fragment, use the code below. `LivenessFragment` is the [Fragment](https://developer.android.com/guide/fragments) representation of the Liveness screen UI.&#x20;

{% hint style="warning" %}
To ensure the license being processed properly, we recommend initializing SDK first, then opening the Liveness screen.
{% endhint %}

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
childFragmentManager.beginTransaction()
    .replace(R.id.content, LivenessFragment.create(actions))
    .commit()
// subscribing to the Fragment result
childFragmentManager.setFragmentResultListener(OzLivenessSDK.Extra.REQUEST_CODE, this) { _, result ->
    when (result.getInt(OzLivenessSDK.Extra.EXTRA_RESULT_CODE)) {
        OzLivenessResultCode.SUCCESS -> { /* start analysis */ }
        else -> { /* show error */ }  
    }
}
```

{% endtab %}

{% tab title="Java" %}

```java
getSupportFragmentManager().beginTransaction()
        .replace(R.id.content, LivenessFragment.Companion.create(actions, null, null, false))
        .addToBackStack(null)
        .commit();
// subscribing to the Fragment result
getSupportFragmentManager().setFragmentResultListener(OzLivenessSDK.Extra.REQUEST_CODE, this, (requestKey, result) -> {
            switch (result.getInt(OzLivenessSDK.Extra.EXTRA_RESULT_CODE)) {
                case OzLivenessResultCode.SUCCESS: {/* start analysis */}
                default: {/* show error */}
            }
        });
```

{% endtab %}
{% endtabs %}

To obtain the captured video, use the`onActivityResult` method:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == REQUEST_CODE) {
      sdkMediaResult = OzLivenessSDK.getResultFromIntent(data)
      sdkErrorString = OzLivenessSDK.getErrorFromIntent(data)
    }
}
```

{% endtab %}

{% tab title="Java" %}

```java
@Override
protected void onActivityResult(int requestCode, int resultCode, @androidx.annotation.Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE) {
        List<OzAbstractMedia> sdkMediaResult = OzLivenessSDK.INSTANCE.getResultFromIntent(data);
        String sdkErrorString = OzLivenessSDK.INSTANCE.getErrorFromIntent(data);
    }
```

{% endtab %}
{% endtabs %}

* `sdkMediaResult` – an object with video capturing results for interactions with Oz API (a list of the [OzAbstractMedia](https://doc.ozforensics.com/oz-knowledge/guides/developer-guide/sdk/oz-mobile-sdk/android-sdk-methods-and-properties#h.s2ye0si7dzc2) objects),
* `sdkErrorString` – description of [errors](https://doc.ozforensics.com/oz-knowledge/guides/developer-guide/sdk/oz-mobile-sdk/android-sdk-methods-and-properties#error-description), if any.

{% hint style="info" %}
If you use our SDK just for capturing videos, omit the Checking Liveness and Face Biometry step.
{% endhint %}

If a user closes the capturing screen manually, `resultCode` receives the `Activity.RESULT_CANCELED` value.

Code example:

```kotlin
when (resultCode) {
    Activity.RESULT_CANCELED -> *USER CLOSED THE SCREEN*
    OzLivenessResultCode.SUCCESS -> {
        val sdkMediaResult = OzLivenessSDK.getResultFromIntent(data)
        *SUCCESS*
    }
    else -> {
        val errorMessage = OzLivenessSDK.getErrorFromIntent(data)
        *FAILURE*
    }
}
```
