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); }
private void UpdateMediaMetadataCompat(MediaMetadataRetriever metaRetriever = null) { if (_mediaSessionCompat != null) { MainActivity.Instance.RunOnUiThread(() => { MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder(); var item = _mediaSessionCompat.SessionToken; if (metaRetriever != null) { builder .PutString(MediaMetadata.MetadataKeyAlbum, metaRetriever.ExtractMetadata(MetadataKey.Album)) .PutString(MediaMetadata.MetadataKeyArtist, metaRetriever.ExtractMetadata(MetadataKey.Artist)) .PutString(MediaMetadata.MetadataKeyDisplayTitle, metaRetriever.ExtractMetadata(MetadataKey.Title)); } else { builder .PutString(MediaMetadata.MetadataKeyAlbum, _queue[_pos].Album) .PutString(MediaMetadata.MetadataKeyArtist, _queue[_pos].Artist) .PutString(MediaMetadata.MetadataKeyDisplayTitle, _queue[_pos].Title); } if (!String.IsNullOrEmpty(_queue[_pos].Image.ToString())) { Bitmap artwork = BitmapFactory.DecodeFile(_queue[_pos].Image.ToString()); builder.PutBitmap(MediaMetadataCompat.MetadataKeyAlbumArt, artwork); builder.PutBitmap(MediaMetadataCompat.MetadataKeyArt, artwork); } _mediaSessionCompat.SetMetadata(builder.Build()); }); } }
void UpdatePage(string songId) { MediaMetadataRetriever mmr = SongMetadata.GetMetadata(songId); string title = mmr.ExtractMetadata(MetadataKey.Title); string artist = mmr.ExtractMetadata(MetadataKey.Artist); if (title == null || artist == null) { songTitle.Text = AndroidSongsManager.Instance.GetSong(songId).Name.Replace(".mp3", string.Empty); } else { songTitle.Text = title + " - " + artist; } seekbar.CreateSeekBar(); Drawable picture = SongMetadata.GetSongPicture(songId); if (picture != null) { songImg.SetImageDrawable(picture); } else { songImg.SetImageResource(Resource.Drawable.default_song_image); } }
/// <summary> /// Updates the metadata on the lock screen /// </summary> private void UpdateMediaMetadataCompat(MediaMetadataRetriever metaRetriever = null) { if (mediaSessionCompat == null) { return; } MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder(); if (metaRetriever != null) { builder .PutString(MediaMetadata.MetadataKeyAlbum, metaRetriever.ExtractMetadata(MetadataKey.Album)) .PutString(MediaMetadata.MetadataKeyArtist, metaRetriever.ExtractMetadata(MetadataKey.Artist)) .PutString(MediaMetadata.MetadataKeyTitle, metaRetriever.ExtractMetadata(MetadataKey.Title)); } else { builder .PutString(MediaMetadata.MetadataKeyAlbum, mediaSessionCompat.Controller.Metadata.GetString(MediaMetadata.MetadataKeyAlbum)) .PutString(MediaMetadata.MetadataKeyArtist, mediaSessionCompat.Controller.Metadata.GetString(MediaMetadata.MetadataKeyArtist)) .PutString(MediaMetadata.MetadataKeyTitle, mediaSessionCompat.Controller.Metadata.GetString(MediaMetadata.MetadataKeyTitle)); } builder.PutBitmap(MediaMetadata.MetadataKeyAlbumArt, Cover as Bitmap); mediaSessionCompat.SetMetadata(builder.Build()); }
void ExtractMetadata(MediaMetadataRetriever retriever) { int videoWidth = 0; if (int.TryParse(retriever.ExtractMetadata(MetadataKey.VideoWidth), out videoWidth)) { VideoWidth = videoWidth; } int videoHeight = 0; if (int.TryParse(retriever.ExtractMetadata(MetadataKey.VideoHeight), out videoHeight)) { VideoHeight = videoHeight; } long durationMS; string durationString = retriever.ExtractMetadata(MetadataKey.Duration); if (!string.IsNullOrEmpty(durationString) && long.TryParse(durationString, out durationMS)) { DurationTimeSpan = TimeSpan.FromMilliseconds(durationMS); } else { DurationTimeSpan = null; } }
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 async Task <Song> ParseAsync(File path) { var retriever = new MediaMetadataRetriever(); await retriever.SetDataSourceAsync(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 void ExtractMetadata(MediaMetadataRetriever retriever) { _duration = TimeSpan.Zero; _videoWidth = int.Parse(retriever.ExtractMetadata(MetadataKey.VideoWidth)); _videoHeight = int.Parse(retriever.ExtractMetadata(MetadataKey.VideoHeight)); string durationString = retriever.ExtractMetadata(MetadataKey.Duration); if (!string.IsNullOrEmpty(durationString)) { long durationMS = long.Parse(durationString); _duration = TimeSpan.FromMilliseconds(durationMS); } }
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); } } }
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); } }
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 void BuildNotification(String songId) { MediaMetadataRetriever metadata = SongMetadata.GetMetadata(songId); string title = metadata.ExtractMetadata(MetadataKey.Title); string content = metadata.ExtractMetadata(MetadataKey.Artist); if (title == null || content == null) { title = "TubeLoad"; content = AndroidSongsManager.Instance.GetSong(songId).Name.Replace(".mp3", ""); } builder.SetSmallIcon(Resource.Drawable.icon); Drawable drawable = SongMetadata.GetSongPicture(songId); Bitmap bitmap; if (drawable != null) { bitmap = ((BitmapDrawable)SongMetadata.GetSongPicture(songId)).Bitmap; } else { bitmap = BitmapFactory.DecodeResource(Application.Context.Resources, Resource.Drawable.default_song_image); } if (Android.OS.Build.VERSION.SdkInt > Android.OS.BuildVersionCodes.M) { RemoteViews notificationLayoutExpanded = new RemoteViews(Application.Context.PackageName, Resource.Layout.view_notification_actions); notificationLayoutExpanded.SetTextViewText(Resource.Id.notificationTitle, title); notificationLayoutExpanded.SetTextViewText(Resource.Id.notificationContent, content); notificationLayoutExpanded.SetImageViewBitmap(Resource.Id.songImg, bitmap); CreateNotificationMediaActions(notificationLayoutExpanded); builder.SetCustomBigContentView(notificationLayoutExpanded); builder.SetContentTitle(title); } else { builder.SetLargeIcon(bitmap); builder.SetContentTitle(title); builder.SetContentText(content); } songNotification = builder.Build(); notificationManager.Notify(SONG_NOTIFICATION_ID, songNotification); }
public void GetMetadata(string filepath) { MediaMetadataRetriever artistInfo = new MediaMetadataRetriever(); artistInfo.SetDataSource(filepath); Album = artistInfo.ExtractMetadata(MetadataKey.Album); Artist = artistInfo.ExtractMetadata(MetadataKey.Artist); Genre = artistInfo.ExtractMetadata(MetadataKey.Genre); Duration = artistInfo.ExtractMetadata(MetadataKey.Duration); try { byte[] art = artistInfo.GetEmbeddedPicture(); Image = BitmapFactory.DecodeByteArray(art, 0, art.Length); } catch (Exception e) { Image = null; } }
/// <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(); } }
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 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); }
string GetSelectedSongTitle() { MediaMetadataRetriever metadata = SongMetadata.GetMetadata(selectedSong.Id); string title = metadata.ExtractMetadata(MetadataKey.Title); if (title == null) { title = selectedSong.Name.Replace(".mp3", string.Empty); } return(title); }
public override View GetView(int position, View convertView, ViewGroup parent) { var item = lista[position]; View view = convertView; if (view == null) { view = context.LayoutInflater.Inflate(Resource.Layout.poleListy, null); } MediaMetadataRetriever mmr = new MediaMetadataRetriever(); mmr.SetDataSource(context, Android.Net.Uri.Parse(item.Sciezka)); string tytul = mmr.ExtractMetadata(MetadataKey.Title); string wykonawca = mmr.ExtractMetadata(MetadataKey.Artist); string czas = mmr.ExtractMetadata(MetadataKey.Duration); view.FindViewById <TextView>(Resource.Id.tytul).Text = tytul; view.FindViewById <TextView>(Resource.Id.wykonawca).Text = wykonawca; view.FindViewById <TextView>(Resource.Id.czas).Text = new TimeSpan(0, 0, 0, 0, Int32.Parse(czas)).ToString(); return(view); }
private void Initialize() { // Video view. videoView = FindViewById <VideoView>(Resource.Id.videoView); videoViewLayout = FindViewById <RelativeLayout>(Resource.Id.layout_video_view); uri = Android.Net.Uri.Parse("https://raw.githubusercontent.com/mediaelement/mediaelement-files/master/big_buck_bunny.mp4"); videoView.SetVideoURI(uri); videoView.Visibility = ViewStates.Visible; var btnStart = FindViewById <Button>(Resource.Id.btn_start); btnStart.Click += VideoStart; var btnPause = FindViewById <Button>(Resource.Id.btn_pause); btnPause.Click += VideoPause; var btnStop = FindViewById <Button>(Resource.Id.btn_stop); btnStop.Click += VideoStop; // Timeline view timelineView = FindViewById <TimelineView>(Resource.Id.timeline_video); // Range mRootLayout = FindViewById <ViewGroup>(Resource.Id.layout_range); imageViewArrowLeft = mRootLayout.FindViewById <ImageView>(Resource.Id.range_arrow_left); imageViewArrowRight = mRootLayout.FindViewById <ImageView>(Resource.Id.range_arrow_right); imageViewArrowPosition = mRootLayout.FindViewById <ImageView>(Resource.Id.position_arrow); MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever(); mediaMetadataRetriever.SetDataSource(uri.ToString(), new Dictionary <string, string>()); this.videoHeight = Convert.ToInt32(mediaMetadataRetriever.ExtractMetadata(MetadataKey.VideoHeight)); this.videoWidth = Convert.ToInt32(mediaMetadataRetriever.ExtractMetadata(MetadataKey.VideoWidth)); this.videoDuration = Convert.ToInt64(mediaMetadataRetriever.ExtractMetadata(MetadataKey.Duration)) * 1000; }
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)); }
private static int FPS(MediaMetadataRetriever retriever) { int duration = 0; String dur = retriever.ExtractMetadata(MetadataKey.CaptureFramerate); if (dur != null) { duration = int.Parse(dur); } //long h = duration / 3600; //long m = (duration - h * 3600) / 60; //long s = duration - (h * 3600 + m * 60); return(duration); }
private List <TrackModel> GetTracksFromRoot(string[] directoriesPathArray) { foreach (string musicFilePath in directoriesPathArray) { MediaMetadataRetriever mmr = new MediaMetadataRetriever(); if (musicFilePath.EndsWith(".mp3")) { mmr.SetDataSource(musicFilePath); trackModels.Add(new TrackModel { Album = mmr.ExtractMetadata(MetadataKey.Album), Title = mmr.ExtractMetadata(MetadataKey.Title), Artist = mmr.ExtractMetadata(MetadataKey.Artist), Genre = mmr.ExtractMetadata(MetadataKey.Genre), DurationMillisec = mmr.ExtractMetadata(MetadataKey.Duration), Path = musicFilePath }); } } return(trackModels); }
public override void SetDataSource(string filePath) { _reader.SetDataSource(filePath); if (_reader != null) { _infoMp3.Artist = _reader.ExtractMetadata(MetadataKey.Artist) ?? _reader.ExtractMetadata(MetadataKey.Author) ?? _reader.ExtractMetadata(MetadataKey.Composer) ?? "unknown artist"; var lastFilePath = filePath.LastIndexOf("/", StringComparison.Ordinal); var name = filePath.Remove(0, lastFilePath + 1).Replace(".mp3", ""); _infoMp3.NameSong = _reader.ExtractMetadata(MetadataKey.Title) ?? name; long dur = Long.ParseLong(_reader.ExtractMetadata(MetadataKey.Duration)); var seconds = String.ValueOf((dur % 60000) / 1000); var minutes = String.ValueOf((dur / 60000)); _infoMp3.Duration = minutes + ":" + seconds; } }
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 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 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"); } }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.Odtwarzanie); tytul = FindViewById <TextView>(Resource.Id.odtwarzanieTytul); odtworz = FindViewById <Button>(Resource.Id.odtworz); odtworz.Click += Odtworz_Click; zatrzymaj = FindViewById <Button>(Resource.Id.zatrzymaj); zatrzymaj.Click += Zatrzymaj_Click; pb = FindViewById <ProgressBar>(Resource.Id.pasekPostepu); sciezka = Intent.GetStringExtra("bieżący utwór"); MediaMetadataRetriever mmr = new MediaMetadataRetriever(); mmr.SetDataSource(Application.Context, Android.Net.Uri.Parse(sciezka)); string tytulUtowru = mmr.ExtractMetadata(MetadataKey.Title); tytul.Text = tytulUtowru; player = MediaPlayer.Create(this, Android.Net.Uri.Parse(sciezka)); pb.Max = player.Duration; timer = new Timer(x => obsluzPasek(), null, 0, 1000); }
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 string GetVideoDuration(string url) { string timeStr; MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.SetDataSource(url); var duration = retriever.ExtractMetadata(MetadataKey.Duration); int playbackTime = Convert.ToInt32(duration); TimeSpan time = TimeSpan.FromMilliseconds(playbackTime); if (time.Hours > 0) { timeStr = time.ToString(@"hh\:mm\:ss"); } else { timeStr = time.ToString(@"mm\:ss"); } return(timeStr); }