3
votes

I am new to using NAudio so sorry if my question sounds naive.

My project is to play a video file using WPF MediaElement control then access the audio stream of the played video (using NAudio) to represent the audio track of the video while being played (waveform, etc.).

I looked into how to let the NAudio access the audio track of a video file in MediaElement control but could not find a resource..

Any suggestion will be appreciated

1
What did you try ? Can you show some code ?LaGrandMere
I'm afraid this is not a feature of NAudio, and something that the WPF Media Element does not permit in any case. You'd need to go lower-level to get directly to the sound-track of the video. Possibly using WASAPI loopback recording might be good enough for your needs instead?Mark Heath

1 Answers

0
votes

If you are willing to try the preview release of NAudio 1.7 (available on NuGet) it can now read soundtracks out of video files using Media Foundation. Use the MediaFoundationReader class. Here's how you would save the soundtrack to a WAV file:

using (var reader = new MediaFoundationReader("mymovie.m4v"))
{
    WaveFileWriter.CreateWaveFile(reader, "soundtrack.wav");
}

Obviously for waveform drawing you would not need to make a WAV file. Just look at the sample values to get peaks for a waveform renderer.

Note that Media Foundation is only supported on Windows Vista and above.