Skip to content

Instantly share code, notes, and snippets.

@daramkun
Created June 24, 2016 17:28
Show Gist options
  • Save daramkun/324962a7b471a2adf8113f0e2bd18db5 to your computer and use it in GitHub Desktop.
Save daramkun/324962a7b471a2adf8113f0e2bd18db5 to your computer and use it in GitHub Desktop.
namespace ConsoleApplication2
{
class Program
{
static void Main ( string [] args )
{
using ( SharpDX.XAudio2.XAudio2 xaudio2 = new SharpDX.XAudio2.XAudio2 () )
{
using ( System.IO.Stream fileStream = new System.IO.FileStream ( "Test.mp3", System.IO.FileMode.Open ) )
{
using ( SharpDX.MediaFoundation.AudioDecoder audioDecoder = new SharpDX.MediaFoundation.AudioDecoder ( fileStream ) )
{
SharpDX.XAudio2.MasteringVoice masteringVoice = new SharpDX.XAudio2.MasteringVoice ( xaudio2 );
SharpDX.XAudio2.SourceVoice sourceVoice = new SharpDX.XAudio2.SourceVoice ( xaudio2, audioDecoder.WaveFormat );
var samples = audioDecoder.GetSamples ().GetEnumerator ();
if ( samples.MoveNext () )
sourceVoice.SubmitSourceBuffer ( new SharpDX.XAudio2.AudioBuffer ( samples.Current ), null );
bool playEnd = false;
sourceVoice.BufferEnd += ( ptr ) =>
{
if ( samples.MoveNext () ) sourceVoice.SubmitSourceBuffer ( new SharpDX.XAudio2.AudioBuffer ( samples.Current ), null );
else playEnd = true;
};
sourceVoice.Start ();
while ( !playEnd ) ;
sourceVoice.Dispose ();
masteringVoice.Dispose ();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment