展开全部
使用微软的winmm.dll播放音乐
把dll加载到项目中,
public enum PlaySoundFlags : int
{
SND_SYNC = 0x0000,
SND_ASYNC = 0x0001,
SND_NODEFAULT = 0x0002,
SND_MEMORY = 0x0004,
SND_LOOP = 0x0008,
SND_NOSTOP = 0x0010,
SND_NOWAIT = 0x00002000,
SND_ALIAS = 0x00010000,
SND_ALIAS_ID = 0x00110000,
SND_FILENAME = 0x00020000,
SND_RESOURCE = 0x00040004
}
[DllImport("winmm")]
public static extern bool PlaySound(string szSound, IntPtr hMod, PlaySoundFlags flags);
//点击按钮时候,调用PlaySound方法即可
//参数1:要播放的音乐路径
//例如:PlaySound("路径",IntPtr.Zero,PlaySoundFlags.SND_ASYNC);
追问
麻烦问一下,如何加载winmm.dll?是把winmm.dll放在某个文件下么?[DllImport("winmm")]这个语句该如何使用?
追答
winmm.dll放在程序根目录就行
通过DllImport特性的使用,导出dll中的方法
添加引用可以加载dll
展开全部
利用.NET自带的类库SoundPlayer来播放音乐,此类在System.Media命名空间中,支持同步和异步播放,也支持文件和流播放。
同步播放:
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = @"d:\music\happy.mp3";
player.Load();
player.Play();
异步播放:
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = @"d:\music\happy.mp3";
player.LoadAsync();
player.PlaySync();
循环播放:
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = @"d:\music\happy.mp3";
player.Load();
player.PlayLooping();
同步播放:
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = @"d:\music\happy.mp3";
player.Load();
player.Play();
异步播放:
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = @"d:\music\happy.mp3";
player.LoadAsync();
player.PlaySync();
循环播放:
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = @"d:\music\happy.mp3";
player.Load();
player.PlayLooping();
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询