Wednesday, April 10, 2013

[android help] Sending Audio from Android phone to a linux device resulted in a constant scrambled sound


I've written a code that sends voice from an android device's mic to a PC(Ubuntu) through sockets (Static IP).


I've changed the audio format multiple times. The problem is that I receive only scrambled sound.


The Code On the Android:



public class VoiceTransThread implements Runnable{

@SuppressWarnings("deprecation")
public void run(){
int bufferReadResult;

frequency = 8000;
channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
try {

int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration, audioEncoding, bufferSize);

short[] buffer = new short[bufferSize];
audioRecord.startRecording();

while(RecordingPermission){

bufferReadResult = audioRecord.read(buffer, 0, bufferSize);

for (int i = 0;i {

out.println(buffer[i]);

}
}


} catch (Throwable t) {
Log.e("Voice Command","Recording Failed");
}


}


}};


The code in the PC:



ssVoi = new ServerSocket(voiPort);
cVoi = ssVoi.accept();


Format = getAudioFormat();


byte[] tempArray = new byte[10000];
BufferedInputStream bis = new BufferedInputStream(cVoi.getInputStream());
ByteArrayInputStream bais = new ByteArrayInputStream(tempArray);
AudioFormat audioFormat = Format;
ais= new AudioInputStream(bais, audioFormat, tempArray.length / audioFormat.getFrameSize());


SourceDataLine sdl = AudioSystem.getSourceDataLine(Format);
sdl.open();
sdl.start();//start playing whatever is in this source data line


ArrayList byteArrayList = new ArrayList();
boolean interrupted = false;
while (!interrupted) {
int nextByte = 0;
while ((nextByte = bis.read()) != -1 && (byteArrayList.size() < 10000)) {
byteArrayList.add((byte) nextByte);
System.out.println(nextByte);
}
for (int i = 0; i < byteArrayList.size(); i++) {
tempArray[i] = byteArrayList.get(i);
}
byteArrayList.clear();
sdl.write(tempArray, 0, tempArray.length);

}


Please help!! I don't know what am I doing wrong??



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...