In the actual test, we found that the playing sound class described in the previous article (a class to play sound on netcf in CSHARP) runs normally in the PDA, but it cannot work in the PC, after a brief analysis of the cause, it is found that the DLL problem occurs. The DLL used for playing the sound on the PC and PDA is different. In PC, It is winmm, while in PDA it is coredll. The project needs to run on both PC and PDA. Therefore, the dynamic judgment function is added to identify whether the program runs on PC or PDA, to load different DLL files to play the sound, the following is an encapsulation of this class:
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. interopservices;
Namespace minicafe. util
{
Internal class nethelpers
{
[Flags]
Public Enum playsoundflags: int
{
Snd_sync = 0x0000,/* play synchronously (default )*/
Snd_async = 0x0001,/* Play asynchronously */
Snd_nodefault = 0x0002,/* silence (! Default) if sound not found */
Snd_memory = 0x0004,/* pszsound points to a memory file */
Snd_loop = 0x0008,/* loop the sound until next sndplaysound */
Snd_nostop = 0x0010,/* don't stop any currently playing sound */
Snd_nowait = 0x00002000,/* Don't wait if the driver is busy */
Snd_alias = 0x00010000,/* name is a registry alias */
Snd_alias_id = 0x00110000,/* alias is a predefined ID */
Snd_filename = 0x00020000,/* name is file name */
Snd_resource = 0x00040004/* Name Is Resource Name or atom */
}
[Dllimport ("winmm")]
Public static extern bool playsound (string szsound, intptr hmod, playsoundflags flags );
}
Internal class netcfhelpers
{
[Flags]
Public Enum playsoundflags: int
{
Snd_sync = 0x0000,/* play synchronously (default )*/
Snd_async = 0x0001,/* Play asynchronously */
Snd_nodefault = 0x0002,/* silence (! Default) if sound not found */
Snd_memory = 0x0004,/* pszsound points to a memory file */
Snd_loop = 0x0008,/* loop the sound until next sndplaysound */
Snd_nostop = 0x0010,/* don't stop any currently playing sound */
Snd_nowait = 0x00002000,/* Don't wait if the driver is busy */
Snd_alias = 0x00010000,/* name is a registry alias */
Snd_alias_id = 0x00110000,/* alias is a predefined ID */
Snd_filename = 0x00020000,/* name is file name */
Snd_resource = 0x00040004/* Name Is Resource Name or atom */
}
[Dllimport ("coredll")]
Public static extern bool playsound (string szsound, intptr hmod, playsoundflags flags );
}
Public class sound
{
Public static void play (string strfilename)
{
If (Framework. isnetcf)
{
// For PDA
Netcfhelpers. playsound (strfilename, intptr. Zero,
Netcfhelpers. playsoundflags. snd_filename | netcfhelpers. playsoundflags. snd_async );
}
Else
{
// For PC
Nethelpers. playsound (strfilename, intptr. Zero,
Nethelpers. playsoundflags. snd_filename | nethelpers. playsoundflags. snd_async );
}
}
}
}