winform播放音乐

声音文件folder.wav放置在bin目录下debug下
1.通过API调用
复制代码
1using System.Runtime.InteropServices;
2[DllImport("winmm.dll")]
3        public static extern bool PlaySound(string pszSound, int hmod, int fdwSound);
4        public const int SND_FILENAME = 0x00020000;
5        public const int SND_ASYNC = 0x0001
6//调用下面的方法
7PlaySound("folder.wav"0, SND_ASYNC | SND_FILENAME);
8
复制代码
2.通过SoundPlayer对象
1using System.Media;
2
3SoundPlayer p = new SoundPlayer();
4            p.SoundLocation = Application.StartupPath + "\\folder.wav";        
5            p.Load();
6            p.Play();
SoundPlayer
支持同步、异步播放
支持循环播放
支持文件和流播放
同步播放:
1System.Media.SoundPlayer player = new System.Media.SoundPlayer();
2player.SoundLocation = Application.StartupPath + "\\folder.wav";        
3player.Load();
4player.Play();
异步播放:
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = Application.StartupPath + "\\folder.wav";
player.LoadAsync();
player.PlaySync();
循环播放
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = Application.StartupPath + "\\folder.wav";
player.Load();
player.PlayLooping();
3.使用 Audio 对象
1using Microsoft.VisualBasic.Devices;//Microsoft.VisualBasic.dll需要添加引用
2
3Audio a = new Audio();
4          a.Play("folder.wav");

4.通过调用COM组件
“工具”→”自定义控件“→”COM组件“→”window mediaplay “;
然后直接在工具栏中拖拽使用



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值