最经在写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",,); //把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, , );
  mciSendString(@"open ""E:\Music\青花瓷.mp3"" alias temp_alias", null, , );
  mciSendString("play temp_alias repeat", null, , );
  }

关于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#中播放背景音乐几种的方法的更多相关文章

  1. Jquery中each的三种遍历方法

    Jquery中each的三种遍历方法 $.post("urladdr", { "data" : "data" }, function(dat ...

  2. qt qml中PropertyAnimation的几种使用方法

    qml文章 qt qml中PropertyAnimation的几种使用方法 动画应用场景有以下几种: 首先如果一个Rectangle.动画是要改变它的x和y值 1,Rectangle一旦被创建,就要移 ...

  3. JS 中深拷贝的几种实现方法

    JS 中深拷贝的几种实现方法1.使用递归的方式实现深拷贝 //使用递归的方式实现数组.对象的深拷贝 function deepClone1(obj) { //判断拷贝的要进行深拷贝的是数组还是对象,是 ...

  4. stl中map的四种插入方法总结

    stl中map的四种插入方法总结方法一:pair例:map<int, string> mp;mp.insert(pair<int,string>(1,"aaaaa&q ...

  5. python中常用的九种预处理方法

    本文总结的是我们大家在python中常见的数据预处理方法,以下通过sklearn的preprocessing模块来介绍; 1. 标准化(Standardization or Mean Removal ...

  6. Cocos2d-x中播放背景音乐

    背景音乐的播放与停止实例代码如下: SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/Jazz.mp3" ...

  7. C#WinForm中播放背景音乐(亲测可用)

    using System.Runtime.InteropServices; public static uint SND_ASYNC = 0x0001; public static uint SND_ ...

  8. 【转载】JAVA中线程的两种实现方法-实现Runnable接口和继承Thread类

    转自: http://blog.csdn.net/sunguangran/article/details/6069317 非常感谢原作者,整理的这么详细. 在java中可有两种方式实现多线程,一种是继 ...

  9. Java中Map的三种遍历方法

    Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历.   告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...

随机推荐

  1. Ehcache(2.9.x) - Configuration Guide, Configuring Cache

    About Ehcache Configuration Ehcache supports declarative configuration via an XML configuration file ...

  2. 第二十九篇、CoreAnimation的使用

    使用的的三个步骤 1.初始化演员 2.设置好剧情 3.播放 主要类: CALayer // 绘图部分 CABaseAnimation // 基本动画(缩放,移动) CAKeyframeAnimatio ...

  3. Html.ActionLink 几种重载方式说明及例子

    本文整理了该方法的几种重载形式:一 Html.ActionLink("linkText","actionName")该重载的第一个参数是该链接要显示的文字,第二 ...

  4. css笔记——css 实现自定义按钮

    css实现自定义按钮的样式实际上很早就有了,只是会用的人不是很多,里面涉及到了最基础的css写法,在火狐中按钮还是会显示出来,这时需要将i标签的背景设置为白色,同时z-index设置比input高一些 ...

  5. blazeds使用remote访问

     欢迎交流转载,请注明出处:http://www.cnblogs.com/shizhongtao/p/3487128.html 1.配置及说明 jar包说明 从官方上下的Blazeds中,默认的配置有 ...

  6. Base64编码原理与应用

    本文内容转自网络,如需详细内容,请参考相关网址. http://my.oschina.net/goal/blog/201032 代码参考:http://blog.csdn.net/prsniper/a ...

  7. windows下nginx以服务自启动

    1,下载最新版的 Windows Service Wrapper 程序,例如:"winsw-1.9-bin.exe" 也可以修改它的名字,例如:myapp.exe 2, 将重命名后 ...

  8. Nodejs微信接口

    代码重要部分都已详细注释,test.js为实例,如果启动url请求,那么程序默认对json格式数据友好,如果有特殊需要,请自行修改返回数据的处理格式 大概功能简介为下: this._token 提供t ...

  9. CSS的引入方式

    再用HTML编写的文本中,有是没能达到我们想要的效果,此时此刻我们可以用过引用CSS来控制!这不仅使得效果好而且代码层次清晰.CSS的引入方式可以分为四类: 1.链入外部样式表,就是把样式表保存为一个 ...

  10. SublimeText更换皮肤

    SublimeText是一款非常好用的文本编辑工具,官方网址http://www.sublimetext.com/. 这里介绍一下手动安装SublimeText皮肤的方法. (1)首先找一款你喜欢的皮 ...