public static string GetDuration(string mediaFile) { try { string duration; MediaMetadataRetriever retriever; if (mediaFile.Contains("http")) { retriever = new MediaMetadataRetriever(); if ((int)Build.VERSION.SdkInt >= 14) { retriever.SetDataSource(mediaFile, new Dictionary <string, string>()); } else { retriever.SetDataSource(mediaFile); } duration = retriever.ExtractMetadata(MetadataKey.Duration); //time In Millisec retriever.Release(); } else { var file = Android.Net.Uri.FromFile(new Java.IO.File(mediaFile)); retriever = new MediaMetadataRetriever(); //if ((int)Build.VERSION.SdkInt >= 14) // retriever.SetDataSource(file.Path, new Dictionary<string, string>()); //else retriever.SetDataSource(file.Path); duration = retriever.ExtractMetadata(MetadataKey.Duration); //time In Millisec retriever.Release(); } return(duration); } catch (Exception e) { Console.WriteLine(e); return("0"); } }
void UpdateUI() { var uri = ((VideoImagePreview)Element).VideoUrl; var mediaMetadataRetriever = new MediaMetadataRetriever(); try { mediaMetadataRetriever.SetDataSource(uri, new Dictionary <string, string>()); using (var bitmap = mediaMetadataRetriever.GetFrameAtTime(0)) (Forms.Context as Activity).RunOnUiThread(() => ((ImageView)Control).SetImageBitmap(bitmap)); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } finally { mediaMetadataRetriever?.Release(); } }
public ImageSource GenerateThumbImage(string savePath, string url, long usecond) { try { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(url, new Dictionary <string, string>()); Bitmap bitmap = retriever.GetFrameAtTime(usecond <= 0 ? 2 * 1000 * 1000 : usecond, Option.ClosestSync); retriever.Release(); if (bitmap != null) { MemoryStream stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream); byte[] bitmapData = stream.ToArray(); return(ImageSource.FromStream(() => new MemoryStream(bitmapData))); } } catch (Exception) { return(null); } return(null); }