声音播放解决方案(C#)

1、 问题提出

某系统的客户要求添加个新功能:在开场结束时间快到的时候加上声音提示。

2、 寻找解决方案过程

刚接到要求时,心有点没底,因为完全没头绪!发呆了一阵子之后,开始动用网络资源,百度一搜:“.NET 播放声音”,不少相关资料,之后找到了Beep这个方法,百度上说的含糊,不清晰,只好利用VS里面的帮助,找出相关用法。它基于Microsoft.VisualBasic命名空间下的一个方法,但这个方法只能调用系统声音,就发出dong dong的声音!很鬼单调,像是系统出错弹出的警告窗口的声音!继续看看有没有其他更好的方法,在VS里,与声音播放相关的文章还有挺多的,找了个循环播放声音的文章,找到个比较理想的方法,利用My.Computer.Audio.Play(strFileName, AudioPlayMode.BackgroundLoop)实现声音播放功能!心里总算踏实了!糟糕,我用的语言的c#,而这些都是VB下的方法!在C#里是没有这些方法的!郁闷,忙了半天那不是枉了!?再找,被我找到一个家伙Blog里的另一种在C#里实现声音播放的方法:利用DllImport调用外部DLL,扩张对应的入口点!(想理解的请看其博客:http://holym.cnblogs.com/articles/360626.html)

[DllImport("winmm")]

public static extern bool PlaySound(string szSound, IntPtr hMod, PlaySoundFlags flags);

但这个家伙写出了播放的入口点,又没提供Stop的入口点,上网有找不到winmm还有那些入口点!所以这个方法又放弃了!

后来静下来一想,wa kao,.net不是自称无语言障碍的吗?!把VB下实现的声音播放等功能打包成一个DLL文件,在C#里调用不就行了吗!?总算松口气了!

3、 最终解决方案

1)建立一个基于VB语言的类库,里面利用My.Computer.Audio.Play(strFileName, AudioPlayMode.BackgroundLoop)、My.Computer.Audio.Stop()分别实现声音播放和停止功能!

2)在项目了添加引用该类库生成的DLL,即可调用这两个方法了!

4、 应用测试

完整的代码:

1) WinmmVB.vb

 

ExpandedBlockStart.gifContractedBlock.gifPublic Class WinmmVBClass WinmmVB
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub PlayLoopingBackgroundSoundFile()Sub PlayLoopingBackgroundSoundFile(strFileName as String)
InBlock.gif
InBlock.gif        My.Computer.Audio.Play(strFileName, AudioPlayMode.BackgroundLoop)
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub StopBackgroundSound()Sub StopBackgroundSound()
InBlock.gif
InBlock.gif        My.Computer.Audio.Stop()
InBlock.gif
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedBlockEnd.gif
End Class

None.gif
None.gif

2) PlaySoundCS.cs

None.gifusing System;
None.gif
None.gif
using System.Runtime.InteropServices;
None.gif
None.gif 
None.gif
None.gif
namespace Sound
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif    
public class PlaySoundCS
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
public static void Play(string strFileName)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            WinmmVB.WinmmVB obj 
= new WinmmVB.WinmmVB();
InBlock.gif
InBlock.gif            obj.PlayLoopingBackgroundSoundFile(strFileName);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif        
public static void Stop()
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            WinmmVB.WinmmVB obj 
= new WinmmVB.WinmmVB();
InBlock.gif
InBlock.gif            obj.StopBackgroundSound();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}


3) 调用

None.gifSound.PlaySoundCS.Play("23.wav");//23.wav文件与生成文件在同意目录下

 

转载于:https://www.cnblogs.com/yamagl/archive/2007/01/19/624277.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值