Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing MP3 file using C#

Tags:

c#

audio

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.

like image 447
idish Avatar asked Mar 19 '26 15:03

idish


1 Answers

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();
}
like image 91
Mark Heath Avatar answered Mar 21 '26 05:03

Mark Heath