Skip to content

Instantly share code, notes, and snippets.

@kyrathasoft
Created August 8, 2022 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyrathasoft/607c71059fc608224279c3da5ddabfc2 to your computer and use it in GitHub Desktop.
Save kyrathasoft/607c71059fc608224279c3da5ddabfc2 to your computer and use it in GitHub Desktop.
Use NAudio to play .wav or .mp3 files
using System;
using NAudio;
using NAudio.Wave;
namespace PlayMp3{
class Program {
static void Main(string[] args)
{
Console.Clear();
Console.WriteLine("n Press a key to play a 7-second segment from 'Somewhere Out There' (a 1986 song)");
Console.ReadKey();
using(var audioFile = new AudioFileReader("somewhere.mp3"))
using(var outputDevice = new WaveOutEvent())
{
outputDevice.Init(audioFile);
outputDevice.Play();
while (outputDevice.PlaybackState == PlaybackState.Playing)
{
System.Threading.Thread.Sleep(1000);
}
}
Console.WriteLine("nn Press any key to exit...");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment