• 转载: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");  
    
  • 相关阅读:
    用户自定义异常
    触发异常
    第一阶段冲刺终
    第一阶段冲刺七
    第一阶段冲刺六
    第一阶段冲刺五
    第一阶段冲刺四
    Sufficient Statistic (充分统计量)
    DAG-GNN: DAG Structure Learning with Graph Neural Networks
    Masked Gradient-Based Causal Structure Learning
  • 原文地址:https://www.cnblogs.com/woaic/p/5523747.html
Copyright © 2020-2023  润新知