Search code examples
c#.netsoundplayer

How to play all music files stored in ArrayList


I've got an ArrayList which contains a number of sound files as elements. A foreach loop iterates this collection, and plays each note.

The problem is that when the program is run, only the last note plays, but when debugging, it goes through all the elements and each one is played.

The sound is represented as an object of my 'MusicNote' class. I cant understand what the problem is, as when debugging, it works perfectly.


Solution

  • According to http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx player.Play() starts a new thread which means that it returns long before playing the file is finished...

    Use player.PlaySync() instead - either in your main thread or (since it is blocking) on a separate thread.

    Remark: When debugging multi-threaded programs sometimes behave differently - esp. when you step through...