0

I am making a audio chat program, So I tried to sending Audio bytes via web-socket first I did, I get audio bytes and send it on But it was failed(maybe there can't pass completely) second I tried is convert bytes to string with use BitConverter and convert again to byte array with Encoding.UTF8.GetBytes method

this is my code

var pcmAudio = stream.ToByteArray();
var audio = Encoding.UTF8.GetBytes(BitConverter.ToString(pcmAudio));

if I send that 'audio' it works. I can convert to byte array and I can play audio. but, if I send pcmAudio there are an error

Stream ms = new MemoryStream(Encoding.UTF8.GetBytes(data));

above is my receiving audio code. data is string. there's no way to receive with Byte type. So i had to convert data to bytes.

Unfortunately it doesn't work.

the error message is 'Wave header is corrupted'

i want to send byte array compeletly.

ur question 1. Why do you want to send bytes ? You know the way to send audio with bitconverter my answer 1. Byte length would be larger than not converted

thank you

Minwoo Yu
  • 360
  • 2
  • 13
  • Not completely sure what the issue is, but if you convert a byte array to an UTF8 encoded string and back, there is a probability that you are losing information. So the resulting byte array will be different from the original, that's probably why you get a corrupt wave header. Try converting the byte array to a base64 encoded string. This won't lose any information. Use `Convert.ToBase64String()` and `Convert.FromBase64String()`. – lzydrmr Mar 29 '20 at 06:42
  • There are two way to send options. One is Sending Byte array, and the other is send string. If i use 'audio' variable what in the question, it works when I Encoding.UTF8.GetBytes But, When I choose Sending Byte array(send 'pcmAudio' variable) I get data that lost information. – Minwoo Yu Mar 29 '20 at 12:29
  • sending pcmaudio bytes Len : 15344 sending base64 bytes Len : 20460 sending Bitconvert bytes Len: 46031 I check the length of Converted bytes Why the base64 bytes and Bitconverted bytes are larger than law audio?? - thank u – Minwoo Yu Mar 29 '20 at 12:51
  • I don't really get what your problem is. You are saying it 'works'. But what exactly do you want to achieve? - Regarding the length of the encoded string: yes, when you encode a byte array as a base64 string, the length increases. But the encoded string has the advantage of being a string, meaning you can save it easily, send it over the network more easily, it is human readable, and so on. You can convert it back to a byte array and you get the original data back, without losing any information. With a UTF8 encoding, this is not possible in general. – lzydrmr Mar 29 '20 at 16:51

0 Answers0