playing a list of audio files in sequence

2.6k views Asked by At

I've been confronted to a situation. I'm writing a Windows app in C# .NET. I want to play multiple .wav files one after another and do that continuously.

is there a class that could allow that ? otherwise, how would I implement it ?

I tried using MCI and it works, using the notify flag and interception of the MM_MCINOTIFY message but it's rather messy and complicates my code.

2

There are 2 answers

0
John Knoeller On BEST ANSWER

If the list of files is fixed, then just make a playlist and pass it to an instance of MediaPlayer. media player accepts playlist in either .m3u format or .wvx format.

But if you need to choose the order of files dynamically, one if the easiest way is to use PlaySound(). You hand it individual filenames, or pointers to wave data in memory (you would have to marshal that to unmanaged memory). Either dedicate a thread to the sound, and have it just call PlaySound in a loop, or use SND_ASYNC.

Note that it is possible just by looking at the header of a wave file to know how long it will take to play a file with a high degree of accuracy.

[DllImport("WinMM.dll")]    
public static extern bool  PlaySound(string name, int hModule, int flags);

// see mmsystem.h for these flags
public int SND_SYNC      = 0x00000000; // don't return until playback is done
public int SND_ASYNC     = 0x00000001; // play asynchronously    
public int SND_NODEFAULT = 0x00000002; // don't play the default sound if n
public int SND_MEMORY    = 0x00000004; // name is actual a pointer to a WAVEHEADER
public int SND_LOOP      = 0x00000008; // loop the sound until next call to PlaySound (use with SND_ASYNC)
public int SND_NOSTOP    = 0x00000010; // don't stop any currently playing sound
public int SND_FILENAME  = 0x00020000; // name is a filename (it's a sound name otherwise)
public int SND_NOSTOP    = 0x00000010; // don't stop any currently playing sound

Both of these methods will allow you to mix files of different bit depth and sample rate. But they will also both put a slight amount of silence between the files. If you need the transitions between the files to be seamless then you will need to either find a 3rd party audio engine, or roll your own.

Or, you can write a multiple-wave-file file stream reader (in C++, not C# it has to be unmanaged code). And use it instead of the default .wav file reader to play the files in MediaPlayer.

0
Priyank Bolia On