A simple class to play sound on netcf (part 2)

本文介绍了一种在PC和PDA上实现声音播放的方法。通过动态判断运行环境,选择加载不同的DLL(winmm或coredll),确保声音播放功能在两种平台上都能正常工作。提供了具体的C#代码实现。

在实际测试中发现上一片文章(A simple class to play sound on netcf)中介绍的播放声音的类在pda中运行正常,但却无法在pc中工作,简单分析了一下原因,发现是dll的问题,pc和pda播放声音时用的dll不同。pc中是winmm,而pda中则是coredll,项目需要在pc和pda上都可以运行,因此加入了动态判断功能,识别程序运行在pc还是pda中,从而加载不同的dll来播放声音,下面对是该类的一个封装:

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Runtime.InteropServices;
None.gif
None.gif
namespace MiniCafe.Util
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
internal class NetHelpers
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [Flags]
InBlock.gif        
public enum PlaySoundFlags : int
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_SYNC 
= 0x0000/**//* play synchronously (default) */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_ASYNC 
= 0x0001/**//* play asynchronously */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_NODEFAULT 
= 0x0002/**//* silence (!default) if sound not found */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_MEMORY 
= 0x0004/**//* pszSound points to a memory file */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_LOOP 
= 0x0008/**//* loop the sound until next sndPlaySound */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_NOSTOP 
= 0x0010/**//* don't stop any currently playing sound */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_NOWAIT 
= 0x00002000/**//* don't wait if the driver is busy */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_ALIAS 
= 0x00010000/**//* name is a registry alias */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_ALIAS_ID 
= 0x00110000/**//* alias is a predefined ID */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_FILENAME 
= 0x00020000/**//* name is file name */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_RESOURCE 
= 0x00040004 /**//* name is resource name or atom */
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DllImport(
"winmm")]
InBlock.gif        
public static extern bool PlaySound(string szSound, IntPtr hMod, PlaySoundFlags flags);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
internal class NetCFHelpers
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [Flags]
InBlock.gif        
public enum PlaySoundFlags : int
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_SYNC 
= 0x0000/**//* play synchronously (default) */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_ASYNC 
= 0x0001/**//* play asynchronously */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_NODEFAULT 
= 0x0002/**//* silence (!default) if sound not found */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_MEMORY 
= 0x0004/**//* pszSound points to a memory file */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_LOOP 
= 0x0008/**//* loop the sound until next sndPlaySound */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_NOSTOP 
= 0x0010/**//* don't stop any currently playing sound */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_NOWAIT 
= 0x00002000/**//* don't wait if the driver is busy */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_ALIAS 
= 0x00010000/**//* name is a registry alias */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_ALIAS_ID 
= 0x00110000/**//* alias is a predefined ID */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_FILENAME 
= 0x00020000/**//* name is file name */
ExpandedSubBlockStart.gifContractedSubBlock.gif            SND_RESOURCE 
= 0x00040004 /**//* name is resource name or atom */
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DllImport(
"coredll")]
InBlock.gif        
public static extern bool PlaySound(string szSound, IntPtr hMod,PlaySoundFlags flags);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public class Sound
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static void Play(string strFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (Framework.IsNetCF)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//for pda
InBlock.gif
                NetCFHelpers.PlaySound(strFileName, IntPtr.Zero,
InBlock.gif                NetCFHelpers.PlaySoundFlags.SND_FILENAME 
| NetCFHelpers.PlaySoundFlags.SND_ASYNC);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//for pc
InBlock.gif
                NetHelpers.PlaySound(strFileName, IntPtr.Zero,
InBlock.gif                NetHelpers.PlaySoundFlags.SND_FILENAME 
| NetHelpers.PlaySoundFlags.SND_ASYNC); 
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif

转载于:https://www.cnblogs.com/swnuwangyun/archive/2006/11/10/556822.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值