void GetMetaData(global::Android.Net.Uri uri, IDictionary <string, string> headers) { Task.Run(() => { var retriever = new MediaMetadataRetriever(); if (uri.Scheme != null && uri.Scheme.StartsWith("http") && headers != null) { try { retriever.SetDataSource(uri.ToString(), headers); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } } else { retriever.SetDataSource(Context, uri); } ExtractMetadata(retriever); MetadataRetrieved?.Invoke(this, EventArgs.Empty); }); }
private void GetMetaData(global::Android.Net.Uri uri, IDictionary <string, string> headers) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); if (uri.Scheme.StartsWith("http")) { retriever.SetDataSource(uri.ToString(), headers); } else { retriever.SetDataSource(Context, uri); } ExtractMetadata(retriever); }
public static byte[] GetSelectedMediaData(Activity activity, Android.Net.Uri uri) { try { string path = string.Empty; ContentResolver cr = activity.ContentResolver; string type = cr.GetType(uri); if (type.Contains("video")) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(activity, uri); string duration = retriever.ExtractMetadata(MetadataKey.Duration); retriever.Release(); if (!string.IsNullOrEmpty(duration)) { if (Convert.ToInt32(duration) > 30000) { path = VideoHelper.Trim(1, 30000, VideoHelper.GetMediaRealPath(activity, uri)); return(System.IO.File.ReadAllBytes(path)); } } } var stream = activity.ContentResolver.OpenInputStream(uri); return(stream.ReadAsBytes()); } catch (System.Exception ex) { return(null); } }
public async Task <IMediaModel> GenerateThumbImageFromVideo(DirectoryInfo argCurrentDataFolder, MediaModel argExistingMediaModel, IMediaModel argNewMediaModel) { IMediaModel returnValue = new MediaModel(); await Task.Run(() => { string outFilePath = System.IO.Path.Combine(argCurrentDataFolder.FullName, argNewMediaModel.OriginalFilePath); MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(argExistingMediaModel.MediaStorageFilePath); Bitmap bitmap = retriever.GetFrameAtTime(1000); // try at 1 second as this is often a more flattering image if (bitmap != null) { //Save the bitmap var outStream = new FileStream(outFilePath, FileMode.Create); bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, outStream); outStream.Close(); returnValue = argNewMediaModel; } }); return(returnValue); }
private long getMediaDuration(Uri uri) { var mediaMetadataRetriever = new MediaMetadataRetriever(); mediaMetadataRetriever.SetDataSource(this, uri); var durationStr = mediaMetadataRetriever.ExtractMetadata(MetadataKey.Duration); return long.Parse(durationStr); }
private void GetVideoFrames(int viewWidth) { MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); try { mediaMetadataRetriever.SetDataSource(mVideoUri.ToString(), new Dictionary <string, string>()); // Retrieve media data long videoLengthInMs = Convert.ToInt64(mediaMetadataRetriever.ExtractMetadata(MetadataKey.Duration)) * 1000; // Set thumbnail properties (Thumbs are squares) int thumbWidth = mHeightView; int thumbHeight = mHeightView; int numThumbs = (int)Math.Ceiling(((float)viewWidth) / thumbWidth); long interval = videoLengthInMs / numThumbs; for (int i = 0; i < numThumbs; ++i) { Bitmap bitmap = mediaMetadataRetriever.GetFrameAtTime(i * interval, Android.Media.Option.ClosestSync); bitmap = Bitmap.CreateScaledBitmap(bitmap, thumbWidth, thumbHeight, false); mBitmapList.Add(bitmap); } } catch (Exception ex) { Log.Error("Error", ex.ToString()); } finally { mediaMetadataRetriever.Release(); } }
public static Bitmap GetThumbnailFromVideo(Uri uri, long timeMs) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(uri.ToString()); return(retriever.GetFrameAtTime(timeMs * 1000)); }
public bool NeedCompress(string srcPath, int bitrateMode = 10) { try { //Get video metada var mediaMetadataRetriever = new MediaMetadataRetriever(); mediaMetadataRetriever.SetDataSource(srcPath); string bitrateData = mediaMetadataRetriever.ExtractMetadata(Android.Media.MetadataKey.Bitrate); string videoHeight = mediaMetadataRetriever.ExtractMetadata(Android.Media.MetadataKey.VideoHeight); string videoWidth = mediaMetadataRetriever.ExtractMetadata(Android.Media.MetadataKey.VideoWidth); bitrateMode = bitrateMode == 10 ? bitrateMode10 : bitrateMode2; if (!string.IsNullOrEmpty(bitrateData)) { int bitrate = 0; int.TryParse(bitrateData, out bitrate); bitrate /= 1024; return(bitrate > bitrateMode); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(false); }
public void Start(int pos) { // System.Diagnostics.Debug.WriteLine("Start()"); if (pos >= 0 && pos < _queue.Count && !_isPreparing) { _isPreparing = true; _pos = pos; _getQueuePos(_pos); _player?.Reset(); try { var url = _queue[_pos].Uri; File tempMp3 = File.CreateTempFile(_queue[pos].Artist, ".mp3", CacheDir); tempMp3.DeleteOnExit(); var tt = tempMp3.Length(); FileOutputStream fos = new FileOutputStream(tempMp3); fos.Write(url); fos.Close(); FileInputStream fis = new FileInputStream(tempMp3); MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(tempMp3.Path); long duration = Java.Lang.Long.ParseLong(retriever.ExtractMetadata(MetadataKey.Duration)); _queue[_pos].Duration = duration / 1000; retriever.Release(); _player?.SetDataSource(fis.FD); _player?.PrepareAsync(); } catch (System.Exception e) { System.Diagnostics.Debug.WriteLine(e); } } }
/// <summary> /// /// </summary> /// <param name="dirPath"></param> /// <param name="fileName"></param> /// <param name="url">视频相对路径</param> /// <param name="usecond"></param> public void SaveThumbImage(string dirPath, string fileName, string url, long usecond) { if (string.IsNullOrWhiteSpace(dirPath) || string.IsNullOrWhiteSpace(url)) { return; } try { Java.IO.FileInputStream input = new FileInputStream(dirPath + url); MediaMetadataRetriever retriever = new MediaMetadataRetriever(); //retriever.SetDataSource(url, new Dictionary<string, string>()); retriever.SetDataSource(input.FD); Bitmap bitmap = retriever.GetFrameAtTime(usecond <= 0 ? 2 * 1000 * 1000 : usecond, Option.ClosestSync); if (bitmap != null) { MemoryStream stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream); byte[] bitmapData = stream.ToArray(); System.IO.File.WriteAllBytes(dirPath + fileName, bitmapData); } retriever.Release(); bitmap.Recycle(); } catch (Exception e) { System.Console.WriteLine("===error:" + e); } }
public override void SetVideoPath(string path) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(path); ExtractMetadata(retriever); base.SetVideoPath(path); }
public override void SetVideoURI(global::Android.Net.Uri uri) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); if (uri.Scheme.StartsWith("http")) { retriever.SetDataSource(uri.ToString(), new Dictionary <string, string>()); } else { retriever.SetDataSource(Context, uri); } ExtractMetadata(retriever); base.SetVideoURI(uri); }
public static int GetVideoLength(string filepath = null, Android.Net.Uri inputUri = null) { MediaMetadataRetriever mmr = new MediaMetadataRetriever(); if (inputUri != null) { mmr.SetDataSource(Android.App.Application.Context, inputUri); } else if (filepath != null) { mmr.SetDataSource(filepath); } string durationStr = mmr.ExtractMetadata(MediaMetadataRetriever.MetadataKeyDuration); int millSecond = Convert.ToInt32(durationStr); return(millSecond); }
public byte[] GetVideoThumbnailFromWebUri(string url) { var retriever = new MediaMetadataRetriever(); retriever.SetDataSource(url, new Dictionary <string, string>()); return(GetImageSource(retriever)); }
public byte[] GetVideoThumbnailFromFile(string path) { var retriever = new MediaMetadataRetriever(); retriever.SetDataSource(path); return(GetImageSource(retriever)); }
public static MediaMetadataRetriever GetMetadata(string id) { MediaMetadataRetriever metadata = new MediaMetadataRetriever(); string fileName = FileManager.PATH + AndroidSongsManager.Instance.GetSong(id).Name; metadata.SetDataSource(fileName); return(metadata); }
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"); } }
/// <summary> /// 初始化数据 /// </summary> private void InitData() { // 第一帧图片 Bitmap bitmap = null; // 获取视频第一帧 MediaMetadataRetriever retriever = new MediaMetadataRetriever(); try { Android.Net.Uri uri = Android.Net.Uri.Parse(url); if (Build.VERSION.SdkInt >= Build.VERSION_CODES.IceCreamSandwich) { retriever.SetDataSource(uri.ToString(), new Dictionary <string, string>()); } else { retriever.SetDataSource(uri.ToString()); } // 获取第一帧图片 bitmap = retriever.GetFrameAtTime(0, MediaMetadataRetriever.OptionClosest); imageView_VideoThumbnail.SetImageBitmap(bitmap); progressBar_VideoProgressBar.Visibility = ViewStates.Gone; imageView_VideoPlay.Visibility = ViewStates.Visible; imageView_VideoPause.Visibility = ViewStates.Gone; // 进度条 seekBar_VideoTotal = Convert.ToInt32(retriever.ExtractMetadata((int)MetadataKey.Duration)) / 1000; seekBar_VideoSeekBar.Max = Convert.ToInt32(retriever.ExtractMetadata((int)MetadataKey.Duration)) / 1000; seekBar_VideoSeekBar.Enabled = true; textView_VideoTotalTime.Text = (seekBar_VideoTotal / 60).ToString("00") + " : " + (seekBar_VideoTotal % 60).ToString("00"); retriever.Release(); } catch (Exception) { retriever.Release(); } }
protected void GetMetaData(global::Android.Net.Uri uri, IDictionary <string, string> headers) { Task.Run(() => { var retriever = new MediaMetadataRetriever(); if (uri.Scheme != null && uri.Scheme.StartsWith("http")) { retriever.SetDataSource(uri.ToString(), headers ?? new Dictionary <string, string>()); } else { retriever.SetDataSource(Context, uri); } ExtractMetadata(retriever); MetadataRetrieved?.Invoke(this, EventArgs.Empty); }); }
public override void SetVideoPath(string path) { if (System.IO.File.Exists(path)) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(path); ExtractMetadata(retriever); } base.SetVideoPath(path); }
public TimeSpan GetVideoDuration(string path) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(path); var duration = retriever.ExtractMetadata(MetadataKey.Duration); double durationMillisec = Convert.ToDouble(duration); retriever.Dispose(); return(TimeSpan.FromMilliseconds(durationMillisec)); }
public async Task CompressVideo(string srcPath, string destPath, int bitrateMode = 10) { await Task.Delay(10); try { //Get the video metadata var mediaMetadataRetriever = new MediaMetadataRetriever(); mediaMetadataRetriever.SetDataSource(srcPath); string bitrateData = mediaMetadataRetriever.ExtractMetadata(Android.Media.MetadataKey.Bitrate); string videoHeight = mediaMetadataRetriever.ExtractMetadata(Android.Media.MetadataKey.VideoHeight); string videoWidth = mediaMetadataRetriever.ExtractMetadata(Android.Media.MetadataKey.VideoWidth); //Define default quality compress var videoQuality = VideoQuality.High; bitrateMode = bitrateMode == 10 ? bitrateMode10 : bitrateMode2; if (!string.IsNullOrEmpty(bitrateData)) { int bitrate = 0; int.TryParse(bitrateData, out bitrate); bitrate /= 1024; if (bitrate > bitrateMode2) { float reduce = (float)bitrate / (float)bitrateMode2; if (reduce > 6) { videoQuality = VideoQuality.Low; } else if (reduce > 3) { videoQuality = VideoQuality.Medium; } } } var listener = new CompressionListener(); listener.ProgressPercent += Percent; listener.Fail += Fail; listener.Success += (sender, e) => { Success(this, true); }; VideoCompressor.Start(srcPath, destPath, listener, videoQuality, false, false); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
public int GetVideoLength(MediaFile media) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(media.Path); var length = retriever.ExtractMetadata(MetadataKey.Duration); var lengthseconds = Convert.ToInt32(length) / 1000; TimeSpan t = TimeSpan.FromSeconds(lengthseconds); // var timeformat = t.Seconds; return(t.Seconds); }
public static int GetMediaMetadataRetrieverPropertyInteger(Android.Net.Uri uri, int key, int defaultValue) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(uri.ToString()); string value = retriever.ExtractMetadata(key); if (value == null) { return(defaultValue); } return(int.Parse(value)); }
public Song Parse(File path) { var retriever = new MediaMetadataRetriever(); retriever.SetDataSource(path.Path); var title = retriever.ExtractMetadata(MetadataKey.Title); var album = retriever.ExtractMetadata(MetadataKey.Album); var artist = retriever.ExtractMetadata(MetadataKey.Artist); var albumArtist = retriever.ExtractMetadata(MetadataKey.Albumartist); var coverBitmap = GetCoverFromRetriever(retriever); return(new Song(title, album, artist, path, albumArtist, coverBitmap)); }
private static void GetListAlbumsHelper(File parentDir, ref List <string> albumsList) { List <string> albums = new List <string>(); File[] files = parentDir.ListFiles(); if (files == null) { return; } foreach (var file in files) { if (albumsList.Count >= 10) { return; } if (file.IsDirectory) { GetListAlbumsHelper(file, ref albumsList); } else { if (file.Name.EndsWith(".mp3") || file.Name.EndsWith(".wav") || file.Name.EndsWith(".flac")) { Reader.SetDataSource(file.AbsolutePath); string album = Reader.ExtractMetadata(Android.Media.MetadataKey.Album); if (album != null && !albumsList.Contains(album)) { albumsList.Add(album); } } } } }
public override void SetVideoPath(string path) { base.SetVideoPath(path); if (System.IO.File.Exists(path)) { var retriever = new MediaMetadataRetriever(); Task.Run(() => { retriever.SetDataSource(path); ExtractMetadata(retriever); MetadataRetrieved?.Invoke(this, EventArgs.Empty); }); } }
public string GetTrackDuration(string filePath) { MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever(); metaRetriever.SetDataSource(filePath); string duration = metaRetriever.ExtractMetadata(MetadataKey.Duration); metaRetriever.Release(); TimeSpan t = TimeSpan.FromMilliseconds(Convert.ToDouble(duration)); //duration = string.Format("{0:D2}:{1:D2}:{2:D2}", t.Hours, t.Minutes, t.Seconds); //only need minutes seconds for now duration = string.Format("{0:D2}:{1:D2}", t.Minutes, t.Seconds); return(duration); }
public MemoryStream GenerateThumbImage(string url, long usecond) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(url); Bitmap bitmap = retriever.GetFrameAtTime(usecond); if (bitmap != null) { MemoryStream stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Jpeg, 10, stream); byte[] bitmapData = stream.ToArray(); return(stream); // return ImageSource.FromStream(() => new MemoryStream(bitmapData)); } return(null); }
public ImageSource GenerateThumbImage(string url, long usecond) { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(url, new Dictionary <string, string>()); Bitmap bitmap = retriever.GetFrameAtTime(usecond); if (bitmap != null) { MemoryStream stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream); byte[] bitmapData = stream.ToArray(); return(ImageSource.FromStream(() => new MemoryStream(bitmapData))); } else { return(null); } }