MediaAsset

abstract class VideoKit.Assets.MediaAsset

A media asset refers to a media file on the file system. It provides methods for social sharing and other common tasks that are performed on audio, video, and images.

Creating a Media Asset

The media asset is always created from a set of factory methods:

From a File

/// <summary>
/// Create a media aseet from a file.
/// </summary>
/// <param name="path">Path to media file.</param>
/// <returns>Media asset.</returns>
static Task<MediaAsset> FromFile (string path);

The media asset can be created from a file path.

This also supports blob:// URLs in WebGL.

From a Texture

/// <summary>
/// Create a media aseet from a texture.
/// </summary>
/// <param name="texture">Texture.</param>
/// <returns>Image asset.</returns>
static Task<ImageAsset> FromTexture (Texture2D texture);

An ImageAsset can be created from a texture.

The texture MUST be readable.

From Plain Text

/// <summary>
/// Create a media aseet from plain text.
/// </summary>
/// <param name="text">Text.</param>
/// <returns>Text asset.</returns>
static Task<TextAsset> FromText (string text);

A TextAsset can be created from plain text.

From an Audio Clip

/// <summary>
/// Create a media aseet from an audio clip.
/// </summary>
/// <param name="clip">Audio clip.</param>
/// <returns>Audio asset.</returns>
static async Task<AudioAsset> FromAudioClip (AudioClip clip);

An AudioAsset can be created from an audio clip.

From the Camera Roll

/// <summary>
/// Create a media asset by prompting the user to select an image or video from the camera roll.
/// NOTE: This requires iOS 14+.
/// </summary>
/// <returns>Media asset.</returns>
static Task<MediaAsset?> FromCameraRoll<T> () where T : MediaAsset;

A MediaAsset can be created by prompting the user to select an image or video from the camera roll. Below is an example:

// Load an image from the camera roll
ImageAsset imageAsset = await MediaAsset.FromCameraRoll<ImageAsset> as ImageAsset;
// Load a video from the camera roll
VideoAsset = await MediaAsset.FromCameraRoll<VideoAsset> as VideoAsset;

This method is not supported, and returns null, on macOS and Windows.

Accessing the Media Path

/// <summary>
/// Path to media asset.
/// </summary>
string path { get; }

This is the path to the recorded media file on the file system.

On WebGL, this is a blob URL with form blob:...

Sharing a Media Asset

/// <summary>
/// Share the media asset using the native sharing UI.
/// </summary>
/// <param name="message">Optional message to share with the media asset.</param>
/// <returns>Receiving app bundle ID or `null` if the user did not complete the share action.</returns>
Task<string?> Share (string? message = null);

The media asset can be shared using the native sharing UI. An optional message can be attached to the sharing UI.

Most apps will not be able to share the media asset if it includes a message, so only add one if you know what you're doing.

Native sharing is not supported and does nothing on WebGL or Windows.

Saving to the Camera Roll

/// <summary>
/// Save the media asset to the camera roll.
/// </summary>
/// <param name="album">Optional album to save media asset to.</param>
/// <returns>Whether the asset was successfully saved to the camera roll.</returns>
Task<bool> SaveToCameraRoll (string? album = null);

The media asset can be saved to the camera roll. An optional album can be specified.

On WebGL, this method will download the asset to the user's device.

Saving to the camera roll is not supported on macOS or Windows.

Deleting the Asset

/// <summary>
/// Delete the media asset on disk.
/// </summary>
void Delete ();

The media asset can be deleted on disk.

Prefer using this method over System.IO.File.Delete because it will properly handle deleting the file from browser memory on WebGL.

Last updated