Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am working on a c# application that is supposed to play several soundfiles over different output devices.

After much searching I stumbled over the winmm.dll-API. However, I have trouble to propperly set the output-device befor I play a sound.

Here is my code so far:
C#
//open file
MCI_OPEN_PARMS open = new MCI_OPEN_PARMS();

open.wDeviceID = 0;//no reaction
open.lpstrDeviceType = Marshal.StringToHGlobalAnsi("waveaudio");
open.lpstrElementName = Marshal.StringToHGlobalAnsi(@"test.wav");

IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MCI_OPEN_PARMS)));
Marshal.StructureToPtr(open, ptr, false);

uint err = mciSendCommand(0,
    MCI_OPEN,
   MCI_WAIT | MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,
    ptr);

if (0 != err) ThrowException(err);



open = (MCI_OPEN_PARMS)Marshal.PtrToStructure(ptr, typeof(MCI_OPEN_PARMS));

uint devID = open.wDeviceID;

//play file
MCI_PLAY_PARMS play = new MCI_PLAY_PARMS();

play.dwCallback = IntPtr.Zero;
play.dwFrom = 0;
play.dwTo = 0;

IntPtr ptr2 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MCI_PLAY_PARMS)));
Marshal.StructureToPtr(play, ptr2, false);

err = mciSendCommand(devID,
     MCI_PLAY,
     MCI_NOTIFY, ptr2);

if (0 != err) ThrowException(err);


All the neccesary constants and structures are in the class-definition.

The second line (open.wDeviceID = 0;) should be the one to set the output-device, but no matter what I enter, the sound comes always out of the default-output.

I also used the winmm.dl functions waveOutGetNumDevs and waveOutGetDevCaps to list the avaliable output devices. It schows me, that I have speakers and an external USB-Soundcard, like expected.

Help would be much apreciated.

Best Regards, Marc
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900