0

I want send string byte to speaker something like this:

byte[] bt = {12,32,43,74,23,53,24,54,234,253,153};// example array
var ms = new MemoryStream(bt);
var sound = new System.Media.SoundPlayer();
sound.Stream = ms;
sound.Play();

but I get this exception: my problem pic http://8pic.ir/images/g699b52xe5ap9s8yf0pz.jpg

DrKoch
  • 9,556
  • 2
  • 34
  • 43
m-tech
  • 338
  • 2
  • 14
  • 7
    What the speaker is supposed to do with that *random* series of bytes ? You need to *form* a valid WAV stream. – TLama Jan 12 '15 at 09:31
  • 1
    The SoundPlayer class is expecting a .wav file, not a random stream of bytes. Your byte stream does not have a header that corresponds to a .wav file, hence the error. So you've shown us what you've tried, can you tell us a bit more about exactly what you're trying to do and why? – Nanhydrin Jan 12 '15 at 09:33
  • butnext i want to get byte string from microphone – m-tech Jan 12 '15 at 09:42
  • i want to save bytes that can to be made by analog wave and send them by TCP/IP and play by another speaker – m-tech Jan 12 '15 at 09:46

3 Answers3

1

The first bytes of a WAV stream contain info about length, etc. You have to send this "WAV-Header" as well in the first few bytes.

See http://de.wikipedia.org/wiki/RIFF_WAVE

As you'll see its perfectly possible to compose these few bytes in the header and send them before your raw audio data,

DrKoch
  • 9,556
  • 2
  • 34
  • 43
0

NAudio is best app to play that functionality. use sample app provided.It may help.

Arunkumar Chandrasekaran
  • 1,211
  • 4
  • 21
  • 40
0

You can use some library for reading data from microphone or playing it to speakers. I worked successfuly with: NAudio - http://naudio.codeplex.com/

I would not recommend building a WAV file yourself, it may be too much effort for this. Note that this library (and probably some others, Bass - http://www.un4seen.com is also widely used) also have built in functionality for saving and reading WAV files.

ShayD
  • 920
  • 8
  • 18
  • so you mean i get wave file from mic and send this wave file with TCP/IP and on other computer send to it s speaker? if i can discard heaer and footer wawe file it dot waste width band – m-tech Jan 12 '15 at 10:11
  • If you do not need to save the data, just send it and play - you can easily do it by sending packets over the network, and for this I used NAudio directly. I am not sure how easy it is to play these with Media.SoundPlayer - but playing them with NAUdio is not hard – ShayD Jan 13 '15 at 14:48
  • dear shade i found http://www.alvas.net/alvas.audio,articles.aspx you can consider this.it is very nice example – m-tech Jan 13 '15 at 16:14