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

Related Questions

I have two simple POCO classes; I'm trying to get the MyY property below
I'm probably missing something obvious here but here's what I'm trying to do. From
Sorry this may be blatantly obvious but I have spent all morning trying to
I'm trying to get my EditText field to not be as tall but to
I am trying to establish equality of three equal variables, but the following code
I am pretty sure I am missing something very obvious but I am trying
Possible Duplicate: Android RuntimeException: Unable to instantiate the service Trying to get GCM up
For cleaning up the test files, am trying to do the below. But its
Trying to follow this example. (Section String sorting...) Is there anything obvious that would
I'm trying to use Zlib::Deflate.deflate on a massive file (4 gigs). There are obvious

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.