Are there some free audio libraries for commericial use (like NAudio) 开发者_JAVA百科that is able to play mp3 (but not NAudio!) thanks very much! :)
You can use the built in WMPLib - see this MSDN article on Creating the Windows Media Player Control Programmatically.
At the most basic level you'll need code like this:
private void PlayFile(String url)
{
Player = new WMPLib.WindowsMediaPlayer();
Player.URL = url;
Player.controls.play();
}
However, you also get events to tell you that the play state has changed so you can start playing a new track, or close the form (for example):
Player.PlayStateChange +=
new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange)
private void Player_PlayStateChange(int NewState)
{
if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
{
this.Close();
}
}
It does mean that your application is tied to Windows - so you can't use Mono.
You could try using DirectSound. The entries for DirectSound are removed from the "Add Reference" dialog in VS 2008 or higher (I think) but you can still browse in the GAC for the proper DLLs. Look for the latest version. It's pretty easy, something like:
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);
精彩评论