最经在写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. MongoDB - Introduction to MongoDB, Capped Collections

    Overview Capped collections are fixed-size collections that support high-throughput operations that ...

  2. Android通过LIstView显示文件列表

    [绥江一百]http://www.sj100.net                                                  欢迎,进入绥江一百感谢点击[我的小网站,请大家多 ...

  3. mvc中的webapi

    MVC中 webapi的使用 和 在其他网站中如何来调用(MVC) 1.webapi的路由规则注册在App_Start\WebApiConfig.cs文件中 2.webapi控制器继承父类 apiCo ...

  4. Swift泛型和泛型函数

    泛型(generic)可以使我们在程序代码中定义一些可变的部分,在运行的时候指定.使用泛型可以最大限度地重用代码.保护类型的安全以及提高性能.在Swift集合类中,已经采用了泛型.一.一个问题的思考怎 ...

  5. Xcode 真机无法调试

    关于只能在模拟器上测试不能在真机测试的问题 2. 在 buildSetting 里面搜索bitcode,更改为 No 即可.

  6. KSImageNamed-Xcode插件在xcode 6.4/6.3或其他版本中不能使用解决方案

    大家都知道这个插件很强大,但是现在这个插件最新版貌似只支持xcode7 ,需要修改KSImageNamed-xcode中的一个配置文件,添加uuid才能使他支持xcode6.3或6.4 进入下载的插件 ...

  7. 04_例子讲解:rlViewDemo.exe

    参考资料:http://www.roboticslibrary.org/tutorials/first-steps-windows 使用rlViewDemo对应的快捷方式启动程序,可以看到如下界面: ...

  8. web组件新学--layer

    在之前项目后台管理界面开发中,不知道有layer这样好用的组件,我的内心是这样的的...呀!这个框架有弹框,哈哈哈,好开心,不用自己写遮罩层,不用自己写弹框,好开森.. 当知道有layer之后.... ...

  9. Winform 下拉框绑定问题

    在Winform中下拉框绑定的时候只能读到text属性值,Id的值不管怎么搞都读取不到,所以就百度找到了一种方式: public void CmdBind() { var data = _logic. ...

  10. 正则应用—queryURLParameter()

    在项目中,我们做详情页的时候,需要获取到用户从哪里来点击进来,获取到用户的点击地址,根据不同的地址传进的参数向服务器获取不同的数据,然后加载不同的详情页面. 大部分企业都采用字符串截取的方式,quer ...