当前位置: 移动技术网 > IT编程>开发语言>c# > C#播放背景音乐的方法小结

C#播放背景音乐的方法小结

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例总结了c#播放背景音乐的方法。分享给大家供大家参考。具体分析如下: 最经在写winform程序,其中有用到播放背景音乐 特此收集了一些网上的教程: 1、调用非

本文实例总结了c#播放背景音乐的方法。分享给大家供大家参考。具体分析如下:

最经在写winform程序,其中有用到播放背景音乐

特此收集了一些网上的教程:

1、调用非托管的dll

using system.runtime.interopservices;
//dllimport命名空间的引用
class test //提示音
{
 [dllimport("winmm.dll")]
 public static extern bool playsound(string filename,int mod,int flags);
 public void main()
 {
  playsound(@"d:/qm.wav",0,1);
 //把1替换成9,可连续播放
 }
}

2、播放系统自带声音

system.media.systemsounds.asterisk.play();
system.media.systemsounds.beep.play();
system.media.systemsounds.exclamation.play();
system.media.systemsounds.hand.play();
system.media.systemsounds.question.play();

3、使用system.media.soundplayer播放wav

system.media.soundplayer sp = new soundplayer();
sp.soundlocation = @"d:\10sec.wav";
sp.playlooping();

4、使用mci command string多媒体设备程序接口播放mp3,avi等

using system.runtime.interopservices; 
public static uint snd_async = 0x0001; 
public static uint snd_filename = 0x00020000; 
[dllimport("winmm.dll")] 
public static extern uint mcisendstring(string lpstrcommand, 
string lpstrreturnstring, uint ureturnlength, uint hwndcallback); 
public void play() 
{ 
mcisendstring(@"close temp_alias", null, 0, 0); 
mcisendstring(@"open ""e:\music\青花瓷.mp3"" alias temp_alias",null,0,0); 
mcisendstring("play temp_alias repeat", null, 0, 0); 
}

关于mcisendstring的详细参数说明,请参见msdn,或是 http://blog.csdn.net/psongchao/archive/2007/01/19/1487788.aspx
5、使用axwindowsmediaplayer的com组件来播放
a.加载com组件:toolbox->choose items->com components->windows media player:

b.把windows media player控件拖放到winform窗体中,把axwindowsmediaplayer1中url属性设置为mp3或是avi的文件路径,f5运行。

如何使用windows media player循环播放列表中的媒体文件?

假设我们有一个播放列表,下面的代码可以实现自动循环播放

private void axwindowsmediaplayer1_playstatechange(object sender, axwmplib._wmpocxevents_playstatechangeevent e) 
{ 
 if (axwindowsmediaplayer1.playstate == wmplib.wmpplaystate.wmppsmediaended)
{
thread thread = new thread(new threadstart(playthread));
thread.start();
} 
} 
private void playthread()
{ 
 axwindowsmediaplayer1.url = @"e:\music\someone.avi";
 axwindowsmediaplayer1.ctlcontrols.play();
}

希望本文所述对大家的c#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网