---
name: "processPetForegroundMediaByTemplate"
mode: "kit"
versionRequirements:
  - { name: "AIKit", version: "1.4.3" }
  - { name: "@ray-js/ray", version: "1.7.27" }
platform:
  - "iOS"
  - "Android"
async: true
title: "processPetForegroundMediaByTemplate"
---

## processPetForegroundMediaByTemplate

> [VERSION] AIKit >= 1.4.3 | @ray-js/ray >= 1.7.27

> [PLATFORM] iOS, Android

> ⚡ **Supports Promise** — Returns a Promise when success / fail / complete callbacks are omitted.

### Description

Compose pet foreground media based on a template

### Parameters

| Property | Type | Required | Default | Since | Description |
| --- | --- | --- | --- | --- | --- |
| `templateObject` | `ForegroundMediaTemplateObject` | Yes | - | `1.4.3` | Template object |
| `mediaSource` | `string` | Yes | - | `1.4.3` | Input media data source |
| `outputConfig` | `OutputConfig` | No | - | `1.4.3` | Output configuration |
| `extendParam` | `Record<string, any>` | No | - | `1.4.3` | Extended parameters |
| `complete` | `() => void` | No | - | - | Completion callback (executed on both success and failure) |
| `success` | `(params: Object) => void` ↓see below | No | - | - | Callback on successful API call |
| `fail` | `(params: Object) => void` ↓see below | No | - | - | Failure callback |

#### success callback parameters

| Property | Type | Since | Description |
| --- | --- | --- | --- |
| `outputPath` | `string` | `1.4.3` | Path of the output foreground media |
| `extData` | `Record<string, any>` | `1.4.3` | Extension data |

#### fail callback parameters

| Property | Type | Description |
| --- | --- | --- |
| `errorMsg` | `string` | Error message |
| `errorCode` | `string \| number` | Error code |
| `innerError` | `Object` | Error extension |

##### fail(params).innerError properties

| Property | Type | Description |
| --- | --- | --- |
| `errorCode` | `string \| number` | Error extension code |
| `errorMsg` | `string` | Error extension message |


### Referenced Types

##### `interface` ForegroundMediaTemplateObject

| Property | Type | Since | Description |
| --- | --- | --- | --- |
| `type` | `string` | `1.3.2` | Template type, sourced from the cloud |
| `effect` | `ForegroundMediaTemplateEffectObject` | `1.3.2` | Template effect object |
| `extendParam` | `Record<string, any>` | `1.3.2` | Extended parameters |

##### `interface` OutputConfig

| Property | Type | Since | Description |
| --- | --- | --- | --- |
| `path` | `string` | `1.3.2` | Path of the output foreground media |
| `name` | `string` | `1.3.2` | Filename of the output foreground media |
| `min_duration` | `number` | `1.3.2` | Minimum duration of the output foreground media, in seconds |
| `outputType` | `string` | `1.4.3` | Output type; supports image, audio, video; defaults to image |
| `extendParam` | `Record<string, any>` | `1.3.2` | Extended parameters |

##### `interface` ForegroundMediaTemplateEffectObject

| Property | Type | Since | Description |
| --- | --- | --- | --- |
| `code` | `string` | `1.3.2` | Effect code |
| `name` | `string` | `1.3.2` | Effect name |
| `ouputDuration` | `number` | `1.3.2` | Deprecated; use outputDuration |
| `outputDuration` | `number` | `1.4.3` | Effect output duration, in seconds |
| `outputType` | `string` | `1.4.3` | Effect output type; supports image, audio, video; defaults to image |
| `image` | `string` | `1.3.2` | Effect preview image URL |
| `resource` | `string` | `1.3.2` | Effect resource URL |
| `extendParam` | `Record<string, any>` | `1.3.2` | Extended parameters |


### Examples

#### Basic usage

```tsx
import { ai } from '@ray-js/ray'

const { processPetForegroundMediaByTemplate } = ai;

processPetForegroundMediaByTemplate({
  templateObject: { type: 'example', effect: { code: 'example', name: 'example', ouputDuration: 0, outputDuration: 0, image: 'example', resource: 'example' } },
  mediaSource: 'example',
  success: (result) => {
    console.log('processPetForegroundMediaByTemplate success', result);
  },
  fail: (error) => {
    console.log('processPetForegroundMediaByTemplate fail', error.errorMsg);
  },
});
```
