Search code examples
c#.netmultithreadingsoundplayer

Threading issue when playing music file


What I'm trying to do is play a music file for a specified duration, and then stop playing. However, the whole music file is being played. I cannot use the 'PlaySync()' method as I cannot have the thread being blocked.


Solution

  • maybe something like this

     Task.Factory.StartNew(() =>
       {
        var player = new SoundPlayer();
        player.SoundLocation = "myfile";
        player.Play();
        Thread.Sleep(duration)
        player.Stop();
       });