How to Read and Write WMA Meta Tags Programatically Using C#

Print

Recently I have been working on a voice recording software application. It is a WPF Windows application that has to run on computers running Windows XP and newer.

When I started the project I had to make a choice for the encoding between MP3 and Windows Media Audio. I ended up choosing Windows Media Audio because of licensing issues around the MP3 format.

After doing a bit of research I ended up using the NAudio library by Mark Heath which as far as I am concerned provides an excellent managed abstraction over the various audio technologies available in Windows for doing audio recording and playback

While working on this project, one of the aspects I found to be a little poorly documented was that of managing WMA meta tags (or attributes) while recording or playing back a WMA media file

The following is a quick summary of my findings. First a little bit about writing meta tag attributes when writing a WMA file

How to Write WMA Meta Tags

Using the NAudio WindowsMediaFormat library it is pretty straight-forward to add meta tags to an audio recording via the constructor of the WmaWriter class.

  var targetFileStream = System.IO.File.Create(fileName);
  var codecFormat = selectedCodec;
  var profile = codecFormat.GetProfile();
                                
  var metaData = new List();

  metaData.Add(new NAudio.WindowsMediaFormat.WM_Attr("Title"
                      , NAudio.WindowsMediaFormat.WMT_ATTR_DATATYPE.WMT_TYPE_STRING
                      , "Audio File Title"));
  metaData.Add(new NAudio.WindowsMediaFormat.WM_Attr("Author"
                     , NAudio.WindowsMediaFormat.WMT_ATTR_DATATYPE.WMT_TYPE_STRING
                     , "The Author's Name"));
  metaData.Add(new NAudio.WindowsMediaFormat.WM_Attr("Author"
                     , NAudio.WindowsMediaFormat.WMT_ATTR_DATATYPE.WMT_TYPE_STRING
                     , "some comments about this audio recording"));

  // create the WMA writer object and pass in the array of meta tags
  //   along with the rest of the necessary parameters
  var wmaWriter = new NAudio.WindowsMediaFormat.WmaWriter(targetFileStream    // a file stream object
                                                        , waveIn.WaveFormat   // wave in format
                                                        , profile             // codec profile
                                                        , metaData.ToArray());

How to Read WMA Meta Tags

Reading meta tag attributes is even easier than writing them. It can be acomplished using the WmaStream class, also available in the NAudio WindowsMediaFormat library. The WmaStream provides a nice indexer that can be used to read meta tag attributes based on the attribute name.

  using (var wmaStream = new NAudio.WindowsMediaFormat.WmaStream(fileName))
  {
      titleAttribute = wmaStream["Title"];
      authorAttribute = wmaStream["Author"];
      //  ...
      // read other meta tag attributes
  }
is the founder of Donaq, a software development consulting company with a focus on mobility. You can find Mike on Google+ and on LinkedIn.
Design copyright (c) Miky Dinescu