How do get the length of a video in Xamarin Forms

心不动则不痛 提交于 2020-04-14 06:03:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!