Working with Media Assets
Loading, viewing, and sharing media 🤝
VideoKit provides a lightweight abstraction over media files, called media assets. These assets can be loaded from a file or from the user's camera roll; they can be viewed or played back in Unity; and they can be shared with VideoKit.
Creating a Media Asset
The MediaAsset
class represents a media asset.
Media assets can be loaded in a number of ways:
From a File
Media assets typically refer to media files on the file system. As such, you can create an asset using the path to a media file:
// Create a media asset from a file
string path = "/tmp/video.mp4";
MediaAsset asset = await MediaAsset.FromFile(path);
From the Camera Roll
Media assets can also be created by requesting the user to choose a media asset from their camera roll.
The MediaAsset.FromCameraRoll
method presents the
native gallery picker UI and prompts the user to choose an image or video:
// Create a media asset by prompting the user to choose an image
MediaAsset asset = await MediaAsset.FromCameraRoll(MediaAsset.MediaType.Image);
If the user cancels the interaction before choosing an image or video, the function will return null
.
From a Video Recording
The VideoKitRecorder
component provides the
OnRecordingCompleted
event
which can be used to listen for completed recordings. To register a listener, set the
recordingAction
to
RecordingAction.Custom
:
[GIF here]
Sharing a Media Asset
Media assets can be shared in two ways:
Sharing with the Share Sheet
Media assets can be shared with the native share sheet UI using the
MediaAsset.Share
method:
// Share the asset
string receiver = await asset.Share();
// Print the receiver
Debug.Log($"User shared media asset to: {receiver}");
If the user cancels the interaction before sharing the asset, the function will return null
.
Saving to the Camera Roll
Media assets can be saved to the camera roll using the
MediaAsset.SaveToCameraRoll
:
// Save to the camera roll
bool saved = await asset.SaveToCameraRoll();
// Print the result
Debug.Log($"User saved media asset to camera roll? {saved}");