WinForm开发遇到播放声音的问题
2013-01-25 14:20
232 查看
做一个项目,需要播放声音,于是找了几种方法。
首先用的是Soundplayer,它在.NET 自带的类库 System.Media 下。
Soundplayer这家伙有一个特点就是只能播放一个音频文件,不论你new出多少个Soundplayer,它总是播放最后一个音频。只要其中任何一个Soundplayer被停止,马上就没声音了!
后来就换DirectSound,它需要下载并安装Micrisoft DirectX SDK。
这家伙虽然可以多个音频同时播放,但是,它有一个怪癖,就是只要窗口没有被聚焦,它就Shut up了。
后来又试了AxWindowsMediaPlayer,这个玩意呢它好像只能指定音频路径,但是,我想要直接调用资源文件里面的音频,所以,我抛弃它!
最后,无意中搜到NAudio,于是就开始研究它!
这玩意也是有点毛病,竟然没有循环播放的方法,网上找了老半天也没有人做过这个,倒是不少人抄那篇“用C#和NAudio解码库播放mp3示例”。
只能自己研究了,最终的MyPlayer代码:
首先用的是Soundplayer,它在.NET 自带的类库 System.Media 下。
Soundplayer这家伙有一个特点就是只能播放一个音频文件,不论你new出多少个Soundplayer,它总是播放最后一个音频。只要其中任何一个Soundplayer被停止,马上就没声音了!
后来就换DirectSound,它需要下载并安装Micrisoft DirectX SDK。
这家伙虽然可以多个音频同时播放,但是,它有一个怪癖,就是只要窗口没有被聚焦,它就Shut up了。
后来又试了AxWindowsMediaPlayer,这个玩意呢它好像只能指定音频路径,但是,我想要直接调用资源文件里面的音频,所以,我抛弃它!
最后,无意中搜到NAudio,于是就开始研究它!
这玩意也是有点毛病,竟然没有循环播放的方法,网上找了老半天也没有人做过这个,倒是不少人抄那篇“用C#和NAudio解码库播放mp3示例”。
只能自己研究了,最终的MyPlayer代码:
using System.IO; using NAudio.Wave; partial class MyPlayer { WaveOut player; WaveFileReader reader; WaveStream pcmStream; BlockAlignReductionStream blockAlignedStream; public Stream Stream { set { if(reader != null) reader.Dispose(); if(pcmStream != null) pcmStream.Dispose(); if(blockAlignedStream != null) blockAlignedStream.Dispose(); reader = new WaveFileReader(value); pcmStream = WaveFormatConversionStream.CreatePcmStream(reader); blockAlignedStream = new BlockAlignReductionStream(pcmStream); if(player != null) player.Dispose(); player = new WaveOut(WaveCallbackInfo.FunctionCallback()); player.PlaybackStopped += new System.EventHandler<StoppedEventArgs>(player_PlaybackStopped); } } public MyPlayer(Stream media) { Stream = media; } public MyPlayer() { } public int Looping { get; set; } int timer; public void Play() { timer = 0; if(player != null && player.PlaybackState == PlaybackState.Playing) return; if(blockAlignedStream != null) { blockAlignedStream.Position = 0; player.Init(blockAlignedStream); player.Play(); } } void player_PlaybackStopped(object sender, StoppedEventArgs e) { if(timer >= 0 && (Looping == 0 || Looping < timer)) { blockAlignedStream.Position = 0; player.Init(blockAlignedStream); player.Play(); } timer++; } public void Stop() { timer = int.MinValue; if(player != null) { player.Stop(); } } public void Dispose() { if(reader != null) reader.Dispose(); if(pcmStream != null) pcmStream.Dispose(); if(blockAlignedStream != null) blockAlignedStream.Dispose(); if(player != null) { player.Stop(); player.Dispose(); } } }
相关文章推荐
- openal不能播放声音,自己遇到的问题
- [.NET开发] winform开发中使用datagridview遇到的问题
- Asp.net Winform开发遇到的很奇怪的问题
- CocoStudio开发之背景音乐播放两秒就没声音问题
- 开发时遇到的问题videoview初始化播放会出现几秒黑屏问题
- Android 机顶盒开发播放视频有声音无图像问题
- 关于android视频播放开发中 播放视频只有声音没有图像的问题解决方案
- Winform开发中遇到的问题解决
- 在eclipse 把Modle1和Model2架构改为MVC架构开发Jsp遇到的种种问题
- VS2005+ACCESS2003开发中遇到的“标准表达式中数据类型不匹配”问题解决方法
- 更新phonegap2.9打包的dojo开发页面的ios app遇到的问题
- VS2008+WDK+Win7 32位 驱动开发环境配置中遇到的问题
- iOS开发 iOS11遇到的问题
- iOS 开发过程遇到的问题汇总
- yii2开发遇到的问题以及解决方案
- XCODE4+iOS4.3在开发中遇到的苦逼问题汇总。
- 在利用openssl 进行base64编码开发所遇到的头包含问题
- 开发中遇到的关于classnotfound和resource文件找不到和getresource的路径问题
- CAD二次开发中遇到一个无法解决的问题
- 开发Birt过程中遇到的两个困扰我很久的问题?