zoukankan      html  css  js  c++  java
  • 转载:WinForm中播放声音的三种方法

    转载: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");  
    
  • 相关阅读:
    文件I/O(二)
    linux学习之文件I/O篇(一)
    静态库和共享库
    vim-ide
    CentOS6 vsftpd 安装及优化方法
    Redmine2.5+CentOS6+Apache2
    分享一个TP5实现Create()方法的心得
    Windows证书的生成导出以及使用证书验证文件是否被修改
    如何设置程序UAC控制
    关于C#的可变长参数
  • 原文地址:https://www.cnblogs.com/woaic/p/5523747.html
Copyright © 2011-2022 走看看