问题
How do I get the length of a video using Xamarin Forms? I have used the following link to get the thumbnail from a video, but I need to be able to get the video length
https://forums.xamarin.com/discussion/119450/create-thumbnail-from-video
回答1:
OK, I was able to use my brain and figure this out. It uses a similar method in the link:
For Android:
public string VideoLength(string url)
{
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.SetDataSource(url, new Dictionary<string, string>());
var length = retriever.ExtractMetadata(MetadataKey.Duration);
var lengthseconds = Convert.ToInt32(length) / 1000;
TimeSpan t = TimeSpan.FromSeconds(lengthseconds);
var timeformat = t.ToString();
return timeformat.ToString();
}
For IOS:
public string VideoLength(string url)
{
AVAsset avasset = AVAsset.FromUrl((new Foundation.NSUrl(url)));
var length = avasset.Duration.Seconds.ToString();
var lengthseconds = Convert.ToInt32(length) / 1000;
TimeSpan t = TimeSpan.FromSeconds(lengthseconds);
var timeformat = t.ToString();
return timeformat.ToString();
}
来源:https://stackoverflow.com/questions/56951418/how-do-get-the-length-of-a-video-in-xamarin-forms