Created
August 8, 2022 01:23
-
-
Save kyrathasoft/607c71059fc608224279c3da5ddabfc2 to your computer and use it in GitHub Desktop.
Use NAudio to play .wav or .mp3 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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