3

I'm looking for a way for playing an MP3 file without any 3rd side playing it(Media Player, etc) Is there any way for doing it? Thank you.

idish
  • 3,190
  • 12
  • 53
  • 85
  • possible duplicate of [Playing sounds in Winforms using C#](http://stackoverflow.com/questions/1304223/playing-sounds-in-winforms-using-c-sharp) – Alejandro Jun 14 '14 at 15:34

2 Answers2

8

I have written an open source library called NAudio that can do this:

private IWavePlayer waveOut;
private Mp3FileReader mp3FileReader;

private void PlayMp3()
{
    this.waveOut = new WaveOut(); // or new WaveOutEvent() if you are not using WinForms/WPF
    this.mp3FileReader = new Mp3FileReader("myfile.mp3");
    this.waveOut.Init(mp3FileReader);
    this.waveOut.Play();
    this.waveOut.PlaybackStopped += OnPlaybackStopped;
}

private void OnPlaybackStopped(object sender, EventArgs e)
{
    this.waveOut.Dispose();
    this.mp3FileReader.Dispose();
}
Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • Thank you, I guess that was what I am looking for, the OnPlaybackStopped event is being fired when the song reaches its end? – idish Mar 10 '12 at 10:12
  • @idish, NAudio is nicely documented, and for your questions, simply play with it and you will see the answers out. – Lex Li Mar 10 '12 at 11:13
  • I am using NAudio to play mp3 files, it was playing fine but few moments before it stopped playing songs and there isn't any error being thrown. Here is my code https://pastebin.com/m5NLHfdy – Jamshaid K. Jun 08 '17 at 17:29
0

I don't understand why you want to avoid 3rd party libraries. If you're coding in c# you're probably developing for windows, which normally has winmm.dll. So you can import and use mciSendString like in this example.

However, if you convert the MP3 data to raw data, you can play it using the .NET SoundPlayer class.

Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65