firemonkey convert mp3 stream to hex and reverse

  

i try to save a audio from mobile microphone to a Tmemorystream and then convert it to hex with this function:

function StreamToHexStr(AStream: TStream): string;
const
HexArr: array[0..15] of char =
(‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’A’,’B’,’C’,’D’,’E’,’F’);
var
AByte: Byte;
i: Integer;
begin
SetLength(Result, AStream.Size * 2);
AStream.Position := 0;
for i := 0 to AStream.Size – 1 do
begin
AStream.ReadBuffer(AByte, SizeOf(AByte));
Result[i * 2 + 1] := HexArr[AByte shr 4];
Result[i * 2 + 2] := HexArr[AByte and $0F];
end;
end;

the output is a hexadecimal string

after transfer it a a second device i have to change the hex code to a Tmemorystream and the play it with this function:

Stream := TMemoryStream.Create;
try
Writer := TBinaryWriter.Create(Stream);
try
Writer.Write(TEncoding.UTF8.GetBytes(ReciavedSTR));
finally
Writer.Free;
end;
Stream.SaveToFile( TPath.GetSharedMoviesPath()+’/record2.mp3′ );

but the record2.mp3 is not playable and it just include the converted hex string

what is wrong with my code?

for a simple test when i save the recorded stream to a file with this code

FOutputStream.SaveToFile( TPath.GetSharedMoviesPath()+’/record.mp3′ );

it working good.

update project concept:

the hardware transmitter get 406 hex char from mobile phone on serial port and transfer it to second device,then the receiver deliver the hex string to second mobile phone , the second phone must join the strings and convert them to a hear-able sound.

a sample output of base64 to stream and stream save to file

sample mp3 file

Comments are closed.