Duration of media file RRS feed

  • Question

  • I'm trying to create a media player for the Windows Store. I am using MediaElement to play the file, but I would also like to be able to load multiple songs/videos in a playlist and to display the title of the media file and the length of the file in seconds. The title of the file isn't a problem, but the length apparently is. I need something like that:

    TimeSpan duration = GetFileDuration(mFile.PathToFile);


    int durationOfFileInSeconds = GetFileDuration(mFile.PathToFile);

    where mFile is an object that contains data about the mediaFile.

    This is the XAML code (basic code, I took out the fonts and other design-type properties) that I'm using to display the title and the duration on the screen:

     <ListView x:Name="MediaFilesListView">
                        <TextBlock Text="{Binding DisplayName}"/>
                        <TextBlock Text="{Binding Duration}"/>

    The problem is that I can find the duration of a file only when I load the files in the MediaFile. That means that when I load some songs, the user has to wait for all the songs to be loaded so that he can play the first song. This also means that when the user adds some songs, the song that he is playing will stop until all the songs are being processed. Obviously, this is not the right solution.

    So, how can I do that ? I tried a lot of methods but they don't work because I'm trying do develop for Windows Store. I tried:

    1. using a System.Windows.Media.MediaPlayer to load the files in the background
    2. Microsoft.WindowsAPICodecPack
    3. Shell() and GetDetailsOf()
    4. many other solutions that I find online

    I've also tried to create a new page with a MediaElement just to load files in the MediaElement but the MediaElement.MediaOpened or the MediaElement.MediaFailed events are never fired and while ((mPlayer.NaturalDuration.TimeSpan == new TimeSpan())) ; would run indefinitely.

    Really need some help with this. I've seen media players on the Windows Store that can display the duration of the file, so it's possible. I just don't know how.

    Saturday, December 21, 2013 6:25 PM


All replies