Reputation: 1
Hi I have been tried to record audio through MIC using AudioRecord class and store it in the buffer of size 2048.
And I tried to playback the recorded audio from that buffer itself using AudioTrack class creating another buffer of same size. When I Run the Application, it displays my main.xml file and it shows "Unfortunately app has stopped". It does not record anything too itseems. I just want to play audio from the recording buffer itself. Can anyone help me out? This is my code.
public class Main extends Activity {
class AudRecord
{
public AudioRecord recorder;
public boolean isRecording;
public int SAMPLERATE;
public int CHANNELS;
public int AUDIO_FORMAT;
public int buffer;
public Thread recordingThread;
public AudRecord()
{
recorder = null;
isRecording = false;
SAMPLERATE = 44100;
CHANNELS = AudioFormat.CHANNEL_IN_MONO;
AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
recordingThread = null;
buffer = 2048;
}
public void StartRecording() {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLERATE,
CHANNELS, AUDIO_FORMAT, buffer);
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable()
{
public void run() {
writeAudioData();
}
});
recordingThread.start();
}
void writeAudioData() {
byte data[] = new byte[2048];
while (isRecording) {
recorder.read(data, 0, buffer);
send(data);
}
}
public void send(byte[] data) {
int buffers = 2048;
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, buffers,
AudioTrack.MODE_STREAM);
at.play();
at.write(data, 0, buffers);
at.stop();
at.release();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AudRecord rec= new AudRecord();
rec.StartRecording();
}
}
Upvotes: 0
Views: 846
Reputation: 888
Unfortunately app has stopped this means your XML file has problem.
AudioRecording
Example using MediaRecorder
public class MainActivity extends Activity {
MediaRecorder recorder = new MediaRecorder();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.btnStart)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
start();
}
});
((Button) findViewById(R.id.btnStop)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stop();
}
});
}
public void start()
{
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"yourfilename.3gpp");
recorder.prepare();
recorder.start();
}
public void stop()
{
recorder.stop();
recorder.reset();
recorder.release();
}
}
Upvotes: 0