Click here to Skip to main content
15,749,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on a C# app in Visual Studio 2013 that needs to play audio files in .wav, .mp3 and .wma formats. the .wav and mp3 files play with no problem. .wma files, however, seem to require extra handling and I'm at a loss to find a solution.

Here are the using statements at the top of the project file:

using NAudio;
using NAudio.Wave;
using NAudio.FileFormats.Wav;
using NAudio.FileFormats.Mp3;
using NAudio.WindowsMediaFormat;
using NAudio.MediaFoundation;

And here's the code for playback:

private void PlayIntroScreenAudio()
    byte[] IntroAudioInBytes = Convert.FromBase64String(GameInfo.IntroScreenAudio);
    MemoryStream msIntroAudioStream = new MemoryStream(IntroAudioInBytes, 0, IntroAudioInBytes.Length);
    msIntroAudioStream.Write(IntroAudioInBytes, 0, IntroAudioInBytes.Length);
    msIntroAudioStream.Seek(0, SeekOrigin.Begin);
    msIntroAudioStream.Position = 0;

    if (GameInfo.IntroScreenAudioFileExt == ".wav")
        WaveFileReader wfr = new WaveFileReader(msIntroAudioStream);
    else if (GameInfo.IntroScreenAudioFileExt == ".mp3")
        Mp3FileReader mp3rdr = new Mp3FileReader(msIntroAudioStream);
    else if (GameInfo.IntroScreenAudioFileExt == ".wma")
        WMAFileReader wmafr = new WMAFileReader(msIntroAudioStream);
    IntroAudioIsPlaying = true;
    FinalScoreAudioIsPlaying = QuestionAudioIsPlaying = CARAudioIsPlaying = IARAudioIsPlaying = false;
    btnPlayIntroScreenAudio.Image = Properties.Resources.btnStopIcon;
    btnPlayFinalScoreAudio.Image = btnPlayQuestionAudio.Image = btnPlayCorrectResponseAudio.Image =
        btnPlayIncorrectResponseAudio.Image = Properties.Resources.btnPlayIcon;
    Player.PlaybackStopped += Player_PlaybackStopped;

As you'll probably guess, I get a wiggly line under "(msIntroAudioStream)". I tried adding ".ToString()" inside the parentheses, but the app crashes, since wmafr can't read from a string. What other code do I need to play a .wma file?
Updated 24-Mar-15 14:32pm
manic_drummer 25-Mar-15 17:13pm    
Actually, I tried searching the discussions and other folks seem to have a problem getting it to work as well. OK, so WMAFileReader reads a filepath in the form of a string. But I want to play the .wma file from a memory stream. Is this possible with NAudio? That's what I'm trying to find out. If not, then just say the word and I'm outta here.

1 solution

Here are some tips as you seem new to this:

1. Take notice of the compiler messages
2. Read the documentation
3. Use Google
4. If all else fails go to the correct forum.[^]

This explains WMAFileReader and will probably solve your problem:[^][^]

You will note from the discussions that even after you supply a filename string to the constructor there may still be problems with WMAFileReader.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900