Hi all,
I trying to write app that record voice and send it through tcp socket.
Currently i succeed to record voice to sd card. My problem is that the recording buffer works only for short [] but in order to send it through the socket i should convert it to bytes. When i am trying to convert the short [] to byte i get only noise in the created file on sd card.
Can some one help me to do it ??
This is my code:
I trying to write app that record voice and send it through tcp socket.
Currently i succeed to record voice to sd card. My problem is that the recording buffer works only for short [] but in order to send it through the socket i should convert it to bytes. When i am trying to convert the short [] to byte i get only noise in the created file on sd card.
Can some one help me to do it ??
This is my code:
Code:
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Recorder recorderInstance = new Recorder();
Thread th = new Thread(recorderInstance);
recorderInstance.setFileName(
new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/my2.pcm"));
th.start();
recorderInstance.setRecording(true);
synchronized (this)
{
try
{
this.wait(20000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
recorderInstance.setRecording(false);
try
{
th.join();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
class Recorder implements Runnable
{
private int frequency;
private int channelConfiguration;
private volatile boolean isPaused;
private File fileName;
private volatile boolean isRecording;
private final Object mutex = new Object();
// Changing the sample resolution changes sample type. byte vs. short.
private static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
/**
*
*/
public Recorder() {
super();
this.setFrequency(11025);
this.setChannelConfiguration(AudioFormat.CHANNEL_CONFIGURATION_MONO);
this.setPaused(false);
}
public void run()
{
// Wait until we