如题,C#用system.media播放背景音乐总是显示该文件不是波形文件

解决方案 »

  1.   

    可能真不是波形文件吧
    system.media只支持波形文件
    你可以换其他方式播放背景音乐using System;
    using System.Runtime.InteropServices;
    using System.ComponentModel;namespace Common
    {
    /// <summary>
    /// clsPlaySound 的摘要说明。
    /// </summary>
    public class clsPlaySound
    {
    protected const int SND_SYNC  = 0x0;
    protected const int SND_ASYNC  = 0x1;
    protected const int SND_NODEFAULT  = 0x2;
    protected const int SND_MEMORY  = 0x4;
    protected const int SND_LOOP  = 0x8;
    protected const int SND_NOSTOP  = 0x10;
    protected const int SND_NOWAIT  = 0x2000;
    protected const int SND_ALIAS  = 0x10000;
    protected const int SND_ALIAS_ID  = 0x110000;
    protected const int SND_FILENAME  = 0x20000;
    protected const int SND_RESOURCE  = 0x40004;
    protected const int SND_PURGE  = 0x40;
    protected const int SND_APPLICATION  = 0x80; [DllImport("Winmm.dll", CharSet=CharSet.Auto)]
    protected extern static bool PlaySound(string strFile, IntPtr hMod, int flag ); //播放声音函数
    //strSoundFile   --- 声音文件
    //bSynch         --- 是否同步,如果为True,则播放声音完毕再执行后面的操作,为False,则播放声音的同时继续执行后面的操作
    public static bool PlaySoundFile(string strSoundFile, bool bSynch)
    {
    if(!System.IO.File.Exists(strSoundFile))
    return false;
    int Flags;
    if(bSynch)
    Flags = SND_FILENAME | SND_SYNC;
    else
    Flags = SND_FILENAME | SND_ASYNC; return PlaySound(strSoundFile, IntPtr.Zero, Flags);
    }
     }
    }
      

  2.   

    你怎么确定是wav格式的?不可以,文件格式不是通过扩展名来决定的
      

  3.   

    可以用mciSendString,
     [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
            public static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
            mciSendString("Open " + path + " alias wavType", null, 0, 0);//path是声音文件路径
     mciSendString("Play wavType", null, 0, 0);