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

  • Home
  • SEARCH
  • 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 5981379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:56:08+00:00 2026-05-22T21:56:08+00:00

I have this function: public static void Play(string FileName, bool Async = false) {

  • 0

I have this function:

public static void Play(string FileName, bool Async = false)
    {
        System.Windows.Media.MediaPlayer mp = new System.Windows.Media.MediaPlayer();

        mp.Open(FileName.ToUri());
        mp.Play();
    }

When i call

Play(@"file1.mp3");
Play(@"file2.mp3");
Play(@"file3.mp3");
Play(@"file4.mp3");

all them play at same time.

How can i make MediaPlayer wait the end of the file, to play the next? What the function should like?

EDIT:

public static void Play(Uri FileName, bool Async = false)
    {
        AutoResetEvent a = new AutoResetEvent(false);

        MediaPlayer mp = new MediaPlayer();
        mp.MediaEnded += (o1, p1) =>
        {
            a.Set();
        };

        mp.MediaOpened += (o, p) =>
        {
            int total = Convert.ToInt32(mp.NaturalDuration.TimeSpan.TotalMilliseconds);

            mp.Play();
            if (!Async)
            {
                //System.Threading.Thread.Sleep(total);
                a.WaitOne();
            }
        };

        mp.Open(FileName);
    }
  • 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-22T21:56:09+00:00Added an answer on May 22, 2026 at 9:56 pm

    I will share my Solution with you:

    I created a Window: WindowPlay.xaml

    <Window x:Class="Sistema.Util.WindowPlay"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WindowPlay" 
        AllowsTransparency="True"
        Background="Transparent"
        BorderBrush="Transparent"
        BorderThickness="0"
        ResizeMode="NoResize"
        WindowStyle="None"
        Top="0" Left="0"
        Width="16" Height="16" 
        ShowActivated="False"
        ShowInTaskbar="False"
    >
        <Grid>
            <MediaElement Name="MediaElement1" LoadedBehavior="Play" UnloadedBehavior="Close"
                          MediaEnded="MediaElement1_MediaEnded" />
        </Grid>
    </Window>
    

    WindowPlay.xaml.cs:

    using System;
    using System.Windows;
    
    namespace Sistema.Util
    {
        /// <summary>
        /// Interaction logic for WindowPlay.xaml
        /// </summary>
        public partial class WindowPlay : Window
        {
            public WindowPlay()
            {
                try
                {
                    InitializeComponent();
                }
                catch
                {
                    this.Close();
                }
            }
    
            /// <summary>
            /// Plays the specified file name
            /// </summary>
            /// <param name="FileName">The filename to play</param>
            /// <param name="Async">If True play in background and return immediatelly the control to the calling code. If False, Play and wait until finish to return control to calling code.</param>
            public static void Play(Uri FileName, bool Async = false)
            {
                WindowPlay w = new WindowPlay();
    
                var mp = w.MediaElement1;
    
                if (mp == null)
                {
                    // pode estar sendo fechada a janela
                    return;
                }
                mp.Source = (FileName);
    
                if (Async)
                    w.Show();
                else
                    w.ShowDialog();
            }
    
            private void MediaElement1_MediaEnded(object sender, RoutedEventArgs e)
            {
                this.Close();
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class: public class Test { public static void main(String[] args) {
I have a function that I use to add vectors, like this: public static
I have this function... private string dateConvert(string datDate) { System.Globalization.CultureInfo cultEnGb = new System.Globalization.CultureInfo(en-GB);
I have this static helper function: public static DependencyObject GetParentObject(DependencyObject child) { if (child
I have this method in my db class public function query($queryString) { if (!$this->_connected)
static class MyClass { static void Main() { bool b1 = false; if (
I have the following function that prunes a tree data structure : public static
I have this function in my Javascript Code that updates html fields with their
I have this function in my head: <head> window.onload = function(){ var x =
I have this function from a plugin (from a previous post) // This method

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.