如何使用Xamarin Forms获得视频的长度?我已经使用以下链接从视频中获取缩略图,但是我需要能够获取视频长度
https://forums.xamarin.com/discussion/119450/create-thumbnail-from-video
答案 0 :(得分:1)
好的,我能够动脑筋解决这个问题。在链接中使用了类似的方法:
对于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();
}
对于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();
}