是否有一些商业用的免费音频库(如NAudio)可以播放mp3(但不是NAudio!) 非常感谢! :)
答案 0 :(得分:3)
您可以使用内置的WMPLib - 请参阅Creating the Windows Media Player Control Programmatically上的此MSDN文章。
在最基本的级别,你需要这样的代码:
private void PlayFile(String url)
{
Player = new WMPLib.WindowsMediaPlayer();
Player.URL = url;
Player.controls.play();
}
但是,您还会收到一些事件,告诉您播放状态已更改,以便您可以开始播放新曲目或关闭表单(例如):
Player.PlayStateChange +=
new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange)
private void Player_PlayStateChange(int NewState)
{
if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
{
this.Close();
}
}
这确实意味着您的应用程序与Windows绑定 - 因此您无法使用Mono。
答案 1 :(得分:1)
您可以尝试使用DirectSound。 DirectSound的条目将从VS 2008或更高版本中的“添加引用”对话框中删除(我认为),但您仍然可以在GAC中浏览正确的DLL。寻找最新版本。这很简单,例如:
Device dev = new Device();
dev.SetCooperativeLevel(this.Handle, CooperativeLevel.Priority); // this refers to the form to which DirectSound is tied
SecondaryBuffer sound = new SecondaryBuffer("path\\to\\file", dev);
sound.Volume = /* volume */;
sound.Play(0, BufferPlayFlags.Default);