How to play a sound file

C #, how to play (Pause, Forward ...) sound file (mp3, ogg)? The file can be located on the hard drive or on the Internet.

Is there a library or class that can make my work easier?

+7
c # audio mp3
source share
5 answers

If you don't mind including Microsoft.VisualBasic.dll in your project, you can do it like this:

var audio = new Microsoft.VisualBasic.Devices.Audio(); audio.Play("some file path"); 

If you want to do more complex things, the easiest way I know is to use the Windows Media Player API . You add a DLL and then work with it. The API is pretty awkward, but it works; I used it to make my own music player cover for Windows Media Player for personal use. Here are some useful links to get you started:

Build a website using ASP.NET 2.0 to navigate your music library

Windows Media Object Model

Let the music play!

EDIT:

Since I wrote this, I found an easier way if you don't mind including the WPF classes in your code. WPF (.NET 3.0 and forward) has a MediaPlayer class that wraps Windows Media Player. This means that you don’t need to write your own shell, which is good, because, as I mentioned above, the WMP API is quite awkward and difficult to use.

+7
source share

I would recommend the BASS Library . It can play music files with file files and streaming content. There is also a .NET shell.

+4
source share

Alvas.Audio has a RecordPlayer class with these features:

  public static void TestRecordPlayer() { RecordPlayer rp = new RecordPlayer(); rp.PropertyChanged += new PropertyChangedEventHandler(rp_PropertyChanged); rp.Open(new Mp3Reader(File.OpenRead("in.mp3"))); rp.Play(); rp.Forward(1000); rp.Pause(); } static void rp_PropertyChanged(object sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { case RecordPlayer.StateProperty: RecordPlayer rp = ((RecordPlayer)sender); if (rp.State == DeviceState.Stopped) { rp.Close(); } break; } } 
+2
source share
0
source share

A media player is controlled there - mainly that uses Media Player. You can put this in your program, and there is an API that you can use to manage it. I think this is the best quick fix.

0
source share

All Articles