© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
Matt

❔ MediaPlayer in WPF occasionally fails

        public static void PlaySound(string path)
        {
            Task.Run(() =>
            {
                MediaPlayer player = new();
                Task.Delay(50).Wait();
                Uri uri = new(path);
                player.Open(uri);
                player.Volume = 0.1;
                player.Play();
                Task.Delay((int)player.NaturalDuration.TimeSpan.TotalMilliseconds).Wait();
                player.Stop();
                player.Close();
            });
        }
        public static void PlaySound(string path)
        {
            Task.Run(() =>
            {
                MediaPlayer player = new();
                Task.Delay(50).Wait();
                Uri uri = new(path);
                player.Open(uri);
                player.Volume = 0.1;
                player.Play();
                Task.Delay((int)player.NaturalDuration.TimeSpan.TotalMilliseconds).Wait();
                player.Stop();
                player.Close();
            });
        }

I'm using this fire-forget pattern to play a sound through the media player. Sometimes the sound simply refuses to play. After the app has been open for long enough, all sound completely refuses to play no matter what. Any idea why?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ mediaplayer
C#CC# / help
2y ago
WPF TreeView Fails to load ItemsSource
C#CC# / help
16mo ago
Overlapping sounds with mediaplayer?
C#CC# / help
16mo ago
❔ Closing media with MediaPlayer
C#CC# / help
3y ago