码迷,mamicode.com
首页 > Windows程序 > 详细

转载:WinForm中播放声音的三种方法

时间:2016-05-24 16:55:39      阅读:725      评论:0      收藏:0      [点我收藏+]

标签:

转载:WinForm中播放声音的三种方法

本文是转载的文章。原文出处:http://blog.csdn.net/jijunwu/article/details/4753094

声音文件folder.wav放置在bin目录下debug下

1.通过API调用

using System.Runtime.InteropServices;  
[DllImport("winmm.dll")]  
        public static extern bool PlaySound(string pszSound, int hmod, int fdwSound);  
        public const int SND_FILENAME = 0x00020000;  
        public const int SND_ASYNC = 0x0001;   
//调用下面的方法  
PlaySound("folder.wav", 0, SND_ASYNC | SND_FILENAME);  

2.通过SoundPlayer对象

using System.Media;  
  
SoundPlayer p = new SoundPlayer();  
            p.SoundLocation = Application.StartupPath + "//folder.wav";          
            p.Load();  
            p.Play();  

SoundPlayer

支持同步、异步播放
支持循环播放
支持文件和流播放

同步播放:

    System.Media.SoundPlayer player = new System.Media.SoundPlayer();  
    player.SoundLocation = Application.StartupPath + "//folder.wav";          
    player.Load();  
    player.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 对象

using Microsoft.VisualBasic.Devices;//Microsoft.VisualBasic.dll需要添加引用  
  
Audio a = new Audio();  
          a.Play("folder.wav");  

转载:WinForm中播放声音的三种方法

标签:

原文地址:http://www.cnblogs.com/woaic/p/5523747.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!