The number of slimdx resources on the Internet is very small, and tracing is found.
1. Basic playback functions
Void createdirectsound (guid DeviceID, intptr handle) {// audio device ID _ DS = new directsound (DeviceID); // you need to use the window handle _ DS. setcooperativelevel (handle, cooperativelevel. normal);} void play (string file) {secondarysoundbuffer snd = NULL; // read the WAV audio file using (wavestream Ws = new wavestream (File )) {var DESC = new soundbufferdescription (); DESC. format = ws. format; DESC. flags = bufferflags. globalfocus | bufferflags. controlvolume | bufferflags. controlpositionpolicy | bufferflags. getcurrentposition2; DESC. sizeinbytes = (INT) WS. length; // create a second-level buffer for the sound snd = new secondarysoundbuffer (_ DS, DESC); SND. volume =-200; byte [] buffer = new byte [DESC. sizeinbytes]; ws. read (buffer, 0, buffer. length); SND. write <byte> (buffer, 0, lockflags. none);} SND. play (0, playflags. none );}
2. Callback in playback
AutoreseteventFunctions are similarJava wait/notify.Calling the waitone method is equivalent to wait, so that the current thread waits. Calling the set method is equivalent to calling notify, notifying the thread to continue execution..
Class soundunit {public int ID {Get; private set;} public soundbuffer buffer {Get; private set;} public autoresetevent event {Get; private set;} public soundunit (soundbuffer buffer) {This. id = ++ I; this. buffer = buffer; this. event = new autoresetevent (false) ;}} void play (string file) {secondarysoundbuffer snd = NULL; soundunit su = NULL; // read the WAV audio file using (wavestream Ws = new wavestream (File) {var DESC = new soundbufferdescription (); DESC. format = ws. format; DESC. flags = bufferflags. globalfocus | bufferflags. controlvolume | bufferflags. controlpositionpolicy | bufferflags. getcurrentposition2; DESC. sizeinbytes = (INT) WS. length; // create a second-level buffer for the sound snd = new secondarysoundbuffer (_ DS, DESC); SND. volume =-200; byte [] buffer = new byte [DESC. sizeinbytes]; ws. read (buffer, 0, buffer. length); SND. write & amp; lt; byte & amp; gt; (buffer, 0, lockflags. none); su = new soundunit (SND); SND. setnotifposipositions (new [] {// sets the callback new notificationposition {event = su after the playback ends. event, offset = dsbpn_offsetstop});} SND. play (0, playflags. none); console. writeline ("start playing:" + su. ID); waitfornotification (SU);} void waitfornotification (soundunit unit) {New thread () ==>{// wait until the playback is complete unit. event. waitone (); console. writeline ("playing end:" + unit. ID );}). start ();}
Slimdx directsound Module