惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
量子位
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
V
Visual Studio Blog
雷峰网
雷峰网
T
Tailwind CSS Blog
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
博客园 - 叶小钗
Microsoft Azure Blog
Microsoft Azure Blog
T
The Blog of Author Tim Ferriss
U
Unit 42
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
Y
Y Combinator Blog

博客园 - YD

如何查找某一函数的定义 Ruby 线程--生产者、消费者 Ruby的函数指针 Rails2.0学习--真困难 加班赶工,得不偿失——历史给你上六课 7. 创建Subversion服务之补充 Task Manager 1.1 开源软件:(Task Manager)任务管理-找出自己最需要完成的任务 小说阅读器 2.0 6. 创建 Subversion 服务 小说阅读器,有兴趣的同志可以试一下 不规则窗体的制作 简单 Socket 通信 CRC 校验 C# 实现法 5. 多人协作 C# 中信号量的使用 4. 不要把不必要的文件版本化 3. 从 Repository 中恢复 2. 创建你的 Repository
播放 wave 文件
YD · 2007-09-11 · via 博客园 - YD

现在要播放一个 wave 文件 (*.wav),查了一下网上的代码。一般是用 winmm.dll 中的 PlaySound() 或 snPlaySound 函数。下面代码以 PlaySound() 为例,写了一个 WavePlayer 类。包含两个静态方法,可以播放和停止播放声音文件。

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace PlaySoundProj
{
    
/// <summary>
    
/// This class contains functions to play wave files and stop playing.
    
/// </summary>

    public class WavePlayer
    
{
        
/// <summary>
        
/// Win32 api. This is used to play sound file.
        
/// </summary>

        [DllImport("winmm.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
        
private static extern bool PlaySound(string sound, IntPtr hMod, SoundFlags sf);

        
/// <summary>
        
/// This is used to serve PlaySound().
        
/// </summary>

        [Flags]
        
private enum SoundFlags : int
        
{
            SND_SYNC 
= 0x0000,  /* play synchronously (default) */
            SND_ASYNC 
= 0x0001,  /* play asynchronously */
            SND_NODEFAULT 
= 0x0002,  /* silence (!default) if sound not found */
            SND_MEMORY 
= 0x0004,  /* pszSound points to a memory file */
            SND_LOOP 
= 0x0008,  /* loop the sound until next sndPlaySound */
            SND_NOSTOP 
= 0x0010,  /* don't stop any currently playing sound */
            SND_NOWAIT 
= 0x00002000/* don't wait if the driver is busy */
            SND_ALIAS 
= 0x00010000/* name is a registry alias */
            SND_ALIAS_ID 
= 0x00110000/* alias is a predefined ID */
            SND_FILENAME 
= 0x00020000/* name is file name */
            SND_RESOURCE 
= 0x00040004,  /* name is resource name or atom */
        }


        
/// <summary>
        
/// Play a wave file asynchronously.
        
/// </summary>
        
/// <remarks>
        
/// If the file specified couldn't be found, the function returns false.
        
/// If any error occured, the function returns false.
        
/// On succeeded, returns true.
        
/// </remarks>
        
/// <param name="filePath">The wave </param>
        
/// <returns>True if succeeded, otherwise false.</returns>

        public static bool PlayAsyn(string fileName)
        
{
            
// If the file couldn't be found, returns false.
            if (!System.IO.File.Exists(fileName))
                
return false;

            
// play the sound from the selected filename.
            try
            
{
                
if (!PlaySound(fileName, IntPtr.Zero,
                    SoundFlags.SND_FILENAME 
| SoundFlags.SND_ASYNC))
                    
return false;
            }

            
catch
            
{
                
return false;
            }


            
return true;
        }


        
/// <summary>
        
/// Stop all playing sound.
        
/// </summary>

        public static void Stop()
        
{
            
// Stop playing. Ignores errors.
            try
            
{
                PlaySound(
null, IntPtr.Zero, SoundFlags.SND_ASYNC);
            }

            
catch { }
        }

    }

}

但是如果要播放流呢?根据 Win32 api 上的说明,SND_MEMORY 可以做,但如何做,不知道……
后来又查了一下,原来 Framework 标准库里已经有这样功能的类了,System.Media.Player。它不但可以播放声音文件,还可以播放流。

这样网上有人问的播放资源文件里的声音文件的问题就可以解决了。下面是一段示例。这里 chimes 是嵌入的 wave 文件。

SoundPlayer player = new SoundPlayer(Properties.Resources.chimes);
            player.Play();