c#播放声音文件

C#中声音的播放主要有三种方法:

1.使用API函数。

2.使用SoundPlayer类播放。

3.使用DirectX进行播放。

一、使用API函数进行播放。

  windows操作系统中的winmm.dll文件中封装了声音处理的函数。在C#中我们可以通过平台调用的方式使用这里边的API函数来播放声音。

下边的类中使用了PlaySound、sndPlaySound、mciSendString  3个API函数来播放声音。

其中mciSendString还可以播放mp3格式的声音。

复制代码
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Runtime.InteropServices;
namespace HuaXu
{
public static class WavPlayer
{
    [DllImport("winmm.dll", SetLastError = true)]
    static extern bool PlaySound(string pszSound, UIntPtr hmod, uint fdwSound);
    [DllImport("winmm.dll", SetLastError = true)]
    static extern long mciSendString(string strCommand, 
        StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);
    [DllImport("winmm.dll")]
    private static extern long sndPlaySound(string lpszSoundName, long uFlags);

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

    public static void Play(string strFileName)
    {
        PlaySound(strFileName, UIntPtr.Zero, 
           (uint)(SoundFlags.SND_FILENAME | SoundFlags.SND_SYNC | SoundFlags.SND_NOSTOP));
    }
    public static void mciPlay(string strFileName)
    {
        string playCommand = "open " + strFileName + " type WAVEAudio alias MyWav";
        mciSendString(playCommand, null, 0, IntPtr.Zero);
        mciSendString("play MyWav", null, 0, IntPtr.Zero);

    }
    public static void sndPlay(string strFileName)
    {
        sndPlaySound(strFileName, (long)SoundFlags.SND_SYNC);
    }
}
}
复制代码
复制代码

其中SoundFlags枚举定义了各种播放的方式。

二、使用SoundPlayer播放声音。

System.Media名称空间下的类SoundPlayer 可以让我们很方便的播放wav波形声音文件。SoundPlayer类其实就是对winmm.dll文件中API函数的封装。

SoundPlayer类的使用很简单。如下:

                    SoundPlayer player = new SoundPlayer();
                    player.SoundLocation = "音乐文件名";
                    player.Load();
                    player.Play();

其中Play方法是异步方法,会在另一个线程中播放。如果我们有时候需要等声音播放完毕之后再进行下一步操作。即声音播放需要阻塞当前线程。就可以使用PlaySync()
方法。

SoundPlayer类的缺点:只能播放wav文件;在winxp下播放文件比较大或位率比较高的情况,PlaySync同步播放会有播放不完全的问题。

这个问题的产生是由于winmm.dll的版本问题引起的。在xp下winmm.dll的版本是5。在win7下是6。win7下就没有问题。如果要解决在

xp下播放不完全的问题。可以使用xp下的录音机打开声音文件,把声音文件另存为7kbit/s的位率格式,但这样声音效果就很差了。

 

三、使用DirectX进行播放。

首先要安装DirectX SDK。安装好之后,在C:\Windows\Microsoft.NET\DirectX for Managed Code\1.0.2902.0目录下有在.net下可以使用的dll文件。

首先引用添加引用Microsoft.DirectX.AudioVideoPlayback

using Microsoft.DirectX.AudioVideoPlayback; 

然后实例化Audo类的对象,就可以播放包括mp3格式的音乐文件了。

                Audio audio = new Audio("hello.mp3");
                audio.play();

转载http://www.cnblogs.com/chenjiahong/articles/2716552.html

posted @ 2017-09-29 17:32  左正  阅读(8017)  评论(0编辑  收藏  举报