AdSense

Monday, 17 June 2013

C# - DirectSound

(Deutsche Version) For Panzerkampf I needed sounds. the basic sound play methon in C# is not able to play multiple sounds parallel (the current playing sound will be canceled when a new one is started). Therefor I had to install DirectSound from DirectX. For DirectX, the DirectX SDK is needed.


Previous to the installation, you should remove "Microsoft Visual C++ 2010 Redistributable", otherwise the error "S1023" occurs (here is more information about this error).

After the installation, you only need to add a reference in the C# project (Browse - Microsoft.DirectX.DirectSound, I found the file at C:\Windows\assembly\GAC\Microsoft.DirectX.DirectSound).


Additionally, App.config has to be modified a bit. At first it looked like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>


<startup> has to be modified to <startup useLegacyV2RuntimeActivationPolicy="true">, now everything looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>


The code to play sounds is the following:

Device applicationDevice = new Device();
applicationDevice.SetCooperativeLevel(this, CooperativeLevel.Priority );

soundBuffer1 = new SecondaryBuffer( "c:\afile1.wav", applicationDevice );
soundBuffer2 = new SecondaryBuffer( "c:\afile2.wav", applicationDevice );

soundBuffer1.Play( 0, BufferPlayFlags.Looping );
soundBuffer2.Play( 0, BufferPlayFlags.Looping );


No comments:

Post a Comment