【问题标题】:Play audio files in C#在 C# 中播放音频文件
【发布时间】:2010-11-11 18:28:00
【问题描述】:

我需要播放 mp3 文件。我想使用 winmm.dll (Windows 7)

class Program
{
    [DllImport("winmm.dll")]
    private static extern long mciSendString(string Cmd, StringBuilder StrReturn, int ReturnLength, IntPtr HwndCallback);

    static void Main(string[] args)
    {
        string FileName = @"F:\MUSIC\ROCK.mp3";

        string CommandString = "open " + "\"" + FileName + "\"" + " type mpegvideo alias Mp3File";
        mciSendString(CommandString, null, 0, IntPtr.Zero);
        CommandString = "play Mp3File";
        mciSendString(CommandString, null, 0, IntPtr.Zero);
        Console.ReadKey();
    }
}

但是当我运行我的程序时,什么也没发生。 哪里出错了?

【问题讨论】:

    标签: c# audio mp3


    【解决方案1】:

    接受的答案不适用于包含空格的文件路径。正确的方法是使用你在open命令中设置的别名:

    string FileName = @"F:\MUSIC\ROCK.mp3";
    mciSendString("open \"" + FileName + "\" type mpegvideo alias thisIsMyTag", null, 0, IntPtr.Zero);
    mciSendString("play thisIsMyTag from 0", null, 0, IntPtr.Zero);
    

    【讨论】:

      【解决方案2】:
      string FileName = @"F:\MUSIC\ROCK.mp3";
      mciSendString("open \"" + FileName + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
      mciSendString("play " + FileName + " from 0", null, 0, IntPtr.Zero);
      

      它工作正常。

      【讨论】:

      • 你是提出这个问题的那个谢尔盖吗?
      【解决方案3】:

      您的命令字符串的类型似乎不正确。

      您传递的是type mpegvideo,但该文件不是视频文件。

      正确的音频类型是 type waveaudio 用于 *.wav 文件,type sequencer 用于 *.mid 文件,type cdaudio 用于 RedBook CD。我看不到任何用 MCI 播放 MP3 的方法。您可以尝试完全去掉 type 子句,然后 MCI 会尝试检测它。

      另外,您应该捕获mciSendString返回的错误代码,它可能会为您提供更多信息。

      MSDN Reference

      【讨论】:

        【解决方案4】:

        这里,

        class Program
        {
          public string _command;
          public bool isOpen;
          [DllImport("winmm.dll")]
        
          public static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);
        
        static void Main(string[] args)
        {
            string FileName = @"F:\MUSIC\ROCK.mp3";
            string _command = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
               mciSendString(_command, null, 0, IntPtr.Zero);
             isOpen = true;
        
             if(isOpen)
             {
                _command = "play MediaFile";
                if (loop)
                 _command += " REPEAT";
                mciSendString(_command, null, 0, IntPtr.Zero);
              }
        /*For Close the audio
            _command = "close MediaFile";
            mciSendString(_command, null, 0, IntPtr.Zero);
            isOpen = false; */
        }
        }
        

        CommandString 应该是“play Mediafile”而不是“play Mp3file”希望它会有所帮助.. =]

        【讨论】:

        • 它需要匹配open命令的alias子句中使用的设备ID。它已经在原始代码中做了。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多