I recently developed a plugin for Xamarin.Foms to trim videos. In this post I would like to give a small demonstration on how to use the same. The plugin is published as a Nuget Package : Xamarin.Forms.VideoTrimmer. It’s source code and a demo lives inside a repository in my Github account. At the moment this package targets Android and iOS only.
Task<bool> VideoTrimmerService. Instance. TrimAsync ( int startMS, int lengthMS,
string inputPath, string outputPath )
startMS : starting point of the Trimmed video in milliseconds.
lengthMS : length of the Trimmed video in milliseconds.
inputPath : path of the video to be trimmed.
outputPath : destination path of the trimmed video.
Note: Trimmed video will be in mp4 and mov formats in Android and iOS respectively. outputPath should take care of this.
Sample Xamarin.Forms app to Trim Video
Let’s get our hands dirty. Let’s develop a small application in which we choose a video from the device, trims it and shares the trimmed version to other apps. To start with, create a blank Xamarin.Forms app targeting Android and iOS.
“Pick Video” Button tapping which we will select the video to be trimmed.
“Status” Label which displays what is the action to be taken.
“Starting Time in Seconds” Entry to enter the starting point of trimmed video in seconds.
“Length” Entry to enter the required length of the trimmed video.
“Trim and Share Video” Button to initiate video trimming and sharing the trimmed video to other apps.
Trimming page XAML
In the code behind class for the page, let’s create a method which will check for required permissions and then request permissions that are not already granted. We will be using Permissions API from Xamarin.Essentials.
Now let’s define PickVideo_Clicked method. We will be using Xam.Plugin.Media to pick video.
Now let’s define TrimVideo_Clicked method. We will be using Xamarin.Forms.VideoTrimmer to trim the video and Share API from Xamarin.Essentials to share the trimmed video