C# WPF Jak dysponując ścieżką do utworu bądź filmiku pobrać czas jego trwania?

0

Jak w temacie. Wykombinowałem coś takiego, niestety TimeSpan = NULL. Why?

 public class PlayListTrackData : INotifyPropertyChanged
    {
        public enum eTrackStatus
        {
            ready,
            play,
            error,
            next,
        }
        public PlayListTrackData(string Path)
        {
            this.Name = Path.Substring(Path.LastIndexOf('\\')+1, Path.LastIndexOf('.')-Path.LastIndexOf('\\')-1);
            Uri obj = new Uri(Path);
            MediaElement Track = new MediaElement();
            try
            {
                Track.Source = obj;
                this.Time = Track.NaturalDuration.TimeSpan.ToString(@"mm:ss");
            }
            catch
            {
                this.Time = "00:00";
            }

            this.Path = Path;
            this.TrackStatus = eTrackStatus.ready;
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        // TrackStatus
        private eTrackStatus trackStatus { get; set; }
        public eTrackStatus TrackStatus
        {
            get { return this.trackStatus; }
            set
            {
                this.trackStatus = value;
                if (this.trackStatus == eTrackStatus.error)
                    IconPath = "Resources\\Error16.png";
                if (this.trackStatus == eTrackStatus.next)
                    IconPath = "Resources\\Next16.png";
                if (this.trackStatus == eTrackStatus.play)
                {
                    IconPath = "Resources\\Play16.png";

                }
                if (this.trackStatus == eTrackStatus.ready)
                    IconPath = "Resources\\Ready16.png";
                this.OnPropertyChanged("TrackStatus");
            }
        }
        // Iconpath
        private string iconPath { get; set; }
        public string IconPath
        {
            get { return this.iconPath; }
            private set
            {
                this.iconPath = value;
                this.OnPropertyChanged("IconPath");
            }
        }
        // Name, Path and Time
        public string Name { get; private set; }
        public string Time { get; private set; }
        public string Path { get; private set; }

    } 

Chodzi o konstruktor elementu Playlisty:

public PlayListTrackData(string Path)
        {
            this.Name = Path.Substring(Path.LastIndexOf('\\')+1, Path.LastIndexOf('.')-Path.LastIndexOf('\\')-1);
            Uri obj = new Uri(Path);
            MediaElement Track = new MediaElement();
            try
            {
                Track.Source = obj;
                this.Time = Track.NaturalDuration.TimeSpan.ToString(@"mm:ss");
            }
            catch
            {
                this.Time = "00:00";
            } 
0
try
            {
                Track.Source = obj;
                this.Time = Track.NaturalDuration.TimeSpan.ToString(@"mm:ss");
            }
            catch(Exception ex)
            { // w tej lini kliknij F9
                this.Time = "00:00";
            } 

Odpal i zaczekaj aż Ci wybije, następnie najedź na "ex" tam wszystko będziesz miał opisane dla czego nie działa ;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1