5

The concept behind this program I am trying to make is a novelty audio encryption application. I have very little experience in using the audio libraries within C#. I am trying to record sound which will then be converted to a byte[] in order to undergo some encryption. This will then be saved in a .wav format. I will then need to read in the encrypted audio file and convert this to a byte array to decrypt and then play the decrypted byte array. I know it would be easier to save the file first before performing the encryption/decryption but I don't want an unencrypted version to be saved.

What is the best way/libraries in order to firstly convert the recorded audio to the byte[] and then save. Finally what is the best way to go about playing the decrypted byte[]. Any code / pseudo code would be appreciated.

Thanks

AndrewBennett
  • 65
  • 1
  • 2
  • 5

1 Answers1

12

You can use SoundPlayer class to play audio, and you can provide the data to it using a stream. For your case a MemoryStream would be appropriate solution.

I'll write this by heart:

// Get your sound buffer
byte[] sound = GetMySounds();
// Place the data into a stream
using (MemoryStream ms = new MemoryStream(sound))
{
    // Construct the sound player
    SoundPlayer player = new SoundPlayer(ms);
    player.Play();
}
Nikola Radosavljević
  • 6,871
  • 32
  • 44
  • Brilliant! All seems to be working. Do you know what the best way would be to record the sound and directly convert this to a byte array? Also for people who may look at this thread, SoundPlayer needs to be System.Media.SoundPlayer – AndrewBennett Feb 23 '14 at 23:44
  • I don't know of a way I could describe shortly as playing a sound, but there seem to be several good audio libraries for .NET. Take a look at [NAudio](http://naudio.codeplex.com/) and [following SO question](http://stackoverflow.com/questions/17982468/naudio-record-sound-from-microphone-then-save) – Nikola Radosavljević Feb 24 '14 at 00:19
  • 1
    This works with WAV but not MP3 – Maria Ines Parnisari Apr 06 '17 at 21:02