How to play mp3 files in C#?

前端 未结 7 979
孤独总比滥情好
孤独总比滥情好 2020-12-09 05:13

I\'m trying to play an MP3 file in C# using this guide: http://www.crowsprogramming.com/archives/58

And I\'m doing everything listed, but I still can\'t play any mus

相关标签:
7条回答
  • 2020-12-09 05:29

    The simple way, need following code: 1.In the first add System.Runtime.InteropServices namespase. 2.In class create:

     static class Program
    {
            [DllImport("winmm.dll")]
            private static extern long mciSendString(string strCommand,
                                                     StringBuilder strReturn,
                                                     int iReturnLength,
                                                     IntPtr hwndCallback);
    
            //some code
    
    }
    

    3. Add atribute in the main method:

     [STAThread]
     static void Main(string[] args)
     {//some code}
    

    And then create player method, so:

    public void player()
    {
                   mciSendString("open \"" + "mp3 file path" + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
            mciSendString("play MediaFile", null, 0, IntPtr.Zero);
    }
    
    0 讨论(0)
  • 2020-12-09 05:39

    all you need to do is add a reference to the Window Media Player COM component. You need to add the reference to the file wmp.dll, which you can find in the System32 directory.

    0 讨论(0)
  • 2020-12-09 05:41

    I'm not sure it's still relevant but when I tried it, it only worked when the code ran not in the main thread, i.e., this.InvokeRequired == false

    So, I would advice you try something like:

    ThreadPool.QueueUserWorkItem(
                 delegate(object param)
                 {
                     WMPLib.WindowsMediaPlayer player = new WMPLib.WindowsMediaPlayer();
                     player.URL = "song.mp3";                     
                 });
    

    player.controls.play() is not needed since the player is set to auto play.

    I'm not sure as to why the main thread won't play correctly but I hope this will help.

    0 讨论(0)
  • 2020-12-09 05:44

    I haven't used the Windows Media Player COM object, but here's a link to an alternative method. (I am not the author.) It uses pinvoke to call winmm.dll to play the MP3. I tested it out on Windows Server 2008 and it worked just fine.

    Here's a sample class using the code form the link.

    using System.Runtime.InteropServices;
    
    public class MP3Player
    {
          private string _command;
          private bool isOpen;
          [DllImport("winmm.dll")]
    
          private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);
    
          public void Close()
          {
             _command = "close MediaFile";
             mciSendString(_command, null, 0, IntPtr.Zero);
             isOpen = false;
          }
    
          public void Open(string sFileName)
          {
             _command = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
             mciSendString(_command, null, 0, IntPtr.Zero);
             isOpen = true;
          }
    
          public void Play(bool loop)
          {
             if(isOpen)
             {
                _command = "play MediaFile";
                if (loop)
                 _command += " REPEAT";
                mciSendString(_command, null, 0, IntPtr.Zero);
             }
          }
    }
    
    0 讨论(0)
  • 2020-12-09 05:49

    you may try to remove your code from the "main" event and put it into "form_load" or "button_click" or something like it. cause the way it looks, it should work. also like Robert Seder suggested, you can try to write the whole path for the mp3 file.

    0 讨论(0)
  • 2020-12-09 05:51

    I had the same problem and I solved by setting the player as Static, for example:

    public class MiClass(){
    static WMPLib.WindowsMediaPlayer wplayer;
    private void PlayMusic()
        {
            wplayer = new WMPLib.WindowsMediaPlayer();
            wplayer.URL = "c:\shoryuken.mp3";
            wplayer.controls.play();
        }
    }
    
    0 讨论(0)
提交回复
热议问题