I'm having a problem mixing two different audio recordings into one, just adding bytes of both audio samples.
After that, when I try to open the mixed.mp3 file in the media player, it says:
Windows Media Player encountered a problem while playing a file.
Here is the code I use to mix audio files:
byte[] bytes1,bytes2,final; int length1,length2,max; // Getting byte[] of audio file using ( BinaryReader b = new BinaryReader(File.Open("background.mp3" , FileMode.Open)) ) { length1 = (int)b.BaseStream.Length; bytes1 = b.ReadBytes(length1); } using ( BinaryReader b = new BinaryReader(File.Open("voice.mp3" , FileMode.Open)) ) { length2 = (int)b.BaseStream.Length; bytes2 = b.ReadBytes(length2); } // Getting max length if(length1 > length2){ max = length1; }else{ max = length2; } // Initializing output byte[] of max length final = new byte[max]; // Adding byte1 and byte2 and copying into final for (int i=0;i<max;i++) { byte b1 , b2; if(i < length1){ b1 = bytes1[i]; }else{ b1 = 0; } if ( i < length2 ){ b2 = bytes2[i]; } else{ b2 = 0; } final[i] = (byte)(b1 + b2); } // Writing final[] as an mp3 file File.WriteAllBytes("mixed.mp3" , final);
Note. I tried to mix two identical files, and it worked, that is, the media player did not produce errors and did not play them correctly.
c # audio
TaLha Khan
source share