I am developing an android application, in that i want to record audio and save into a folder.
I have tried this sample code, its reading data from audio object(AudioRecord) but the same application reading only 0 bytes in SAMSUNG GALAXY S5(USB 3.0) Device
Here my code..
private int min_buff;
private int len=0;
private AudioRecord record;
private DataOutputStream dos;
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/demo.mp4");
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button buttonStart = (Button) findViewById(R.id.button1);
final Button buttonStop = (Button) findViewById(R.id.button2);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
buttonStop.setEnabled(false);
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonStart.setEnabled(false);
buttonStop.setEnabled(true);
startDownload();
}
});
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
buttonStart.setEnabled(true);
buttonStop.setEnabled(false);
stopDownload();
}
});
}
public void startDownload(){
OutputStream os = null;
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedOutputStream bos = new BufferedOutputStream(os);
dos = new DataOutputStream(bos);
try {
// Create a DataOuputStream to write the audio data into the saved file.
min_buff = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
record = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, min_buff);
byte[] buffer = new byte[min_buff];
record.startRecording();
while (len < min_buff) {
int bufferReadResult = record.read(buffer, 0, min_buff);
for (int i = 0; i < bufferReadResult; i++){
Log.e("Reading", "Buffer Value :"+buffer[i]);
dos.writeShort(buffer[i]);
}
len += min_buff;
}
record.stop();
dos.close();
}catch (Exception e) {
Log.d("AptioudioRecord","Recording Failed " +e);
}
}
public void stopDownload(){
System.exit(0);
}
and i have also added permission in Manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
While reading record.read(buffer, 0, min_buff);
buffer
filling with 0 only
Please help on this.
Thanks