如何在C#中播放MP3文件? [英] How to play mp3 files in C#?

查看:150
本文介绍了如何在C#中播放MP3文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用本指南在C#中播放MP3文件:
<一href=\"http://www.crowsprogramming.com/archives/58\">http://www.crowsprogramming.com/archives/58

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

而我所做的一切上市,但我仍然无法在我的C#程序播放任何音乐。谁能告诉我什么,我做错了吗?

And I'm doing everything listed, but I still can't play any music in my C# program. Can anyone tell me what I'm doing wrong?

 static void Main(string[] args)
    {
        WMPLib.WindowsMediaPlayer a = new WMPLib.WindowsMediaPlayer();
        a.URL = "song.mp3";
        a.controls.play();
    }

音乐文件宋是在bin文件夹。

The music file "Song" is in the bin folder.

推荐答案

我没有使用Windows Media Player的COM对象,但是的这里是以另一种方法的链接。 (我不是作者。)它使用的PInvoke调用WINMM.DLL播放MP3。我测试了它在Windows Server 2008和它的工作就好了。

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.

下面是一个使用code形成链接的样本类。

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);
         }
      }
}

这篇关于如何在C#中播放MP3文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆