Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 243917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:57:41+00:00 2026-05-11T20:57:41+00:00

It is obvious what I am trying to do below, but I get the

  • 0

It is obvious what I am trying to do below, but I get the following exception:

Unable to return a TimeSpan property value for a Duration value of ‘Automatic’.

I was discouraged to read that

NaturalDuration cannot be determined until after MediaOpened has occurred. (link)

Does this mean that I have to come up with a contrived method to open the file, wait for the media opened event in a separate thread, then return the duration only after the event has fired?

    public static int PlayAudio(string fileName)
    {
        try
        {
            myMediaPlayer.Stop();
            myMediaPlayer.Close();
            myMediaPlayer.Open(new Uri(filename));
            myMediaPlayer.Play();
            return myMediaPlayer.NaturalDuration.TimeSpan.Milliseconds;
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return -1;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:57:41+00:00Added an answer on May 11, 2026 at 8:57 pm

    I have to come up with a contrived method to open the file, wait for the media opened event in a separate thread, then return the duration only after the event has fired

    That’s exactly what it means, unfortunately. The below is what I just came up with for this:

    using System;
    using System.Threading;
    using System.Windows.Media;
    using System.Windows.Threading;
    
    class Program
    {
        static void Main(string[] args)
        {
            var mediaFile = @"c:\path\to\mediafile.mp3";
            var duration = GetMediaDuration(mediaFile, TimeSpan.FromMilliseconds(500));
            Console.WriteLine(duration.ToString());
            Console.ReadKey();
        }
    
        static TimeSpan GetMediaDuration(string mediaFile)
        {
            return GetMediaDuration(mediaFile, TimeSpan.Zero);
        }
    
        static TimeSpan GetMediaDuration(string mediaFile, TimeSpan maxTimeToWait)
        {
            var mediaData = new MediaData() {MediaUri = new Uri(mediaFile)};
    
            var thread = new Thread(GetMediaDurationThreadStart);
            DateTime deadline = DateTime.Now.Add(maxTimeToWait);
            thread.Start(mediaData);
    
            while (!mediaData.Done && ((TimeSpan.Zero == maxTimeToWait) || (DateTime.Now < deadline)))
                Thread.Sleep(100);
    
            Dispatcher.FromThread(thread).InvokeShutdown();
    
            if (!mediaData.Done)
                throw new Exception(string.Format("GetMediaDuration timed out after {0}", maxTimeToWait));
            if (mediaData.Failure)
                throw new Exception(string.Format("MediaFailed {0}", mediaFile));
    
            return mediaData.Duration;
        }
    
        static void GetMediaDurationThreadStart(object context)
        {
            var mediaData = (MediaData) context;
            var mediaPlayer = new MediaPlayer();
    
            mediaPlayer.MediaOpened += 
                delegate 
                    {
                        if (mediaPlayer.NaturalDuration.HasTimeSpan)
                            mediaData.Duration = mediaPlayer.NaturalDuration.TimeSpan; 
                        mediaData.Success = true; 
                        mediaPlayer.Close();
                    };
    
            mediaPlayer.MediaFailed += 
                delegate
                    {
                        mediaData.Failure = true;
                        mediaPlayer.Close();
                    };
    
            mediaPlayer.Open(mediaData.MediaUri);
    
            Dispatcher.Run();
        }
    }
    
    class MediaData
    {
        public Uri MediaUri;
        public TimeSpan Duration = TimeSpan.Zero;
        public bool Success;
        public bool Failure;
        public bool Done { get { return (Success || Failure); } }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you do handle them, how do you justify this… May 12, 2026 at 1:03 am
  • Editorial Team
    Editorial Team added an answer // this conditional is necessary if myType can be an… May 12, 2026 at 1:03 am
  • Editorial Team
    Editorial Team added an answer The InputMask property can contain up to three sections separated… May 12, 2026 at 1:03 am

Related Questions

I would like to pass a parameter defined in the XAML (View) of my
Background In a C# command-line app I'm writing, several of the parameters have yes
I am trying to learn how to use MSBuild so we can use it
I am trying to use the stored procedure mapping feature in Entity Framework to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.