Record and Save Voice | Techbirds

Below is the code for recording a voice through mic and saving it in sdcard :

//Code for saving recording
private void startRecording(){ mRecorder = new MediaRecorder();

File sampleDir = Environment.getExternalStorageDirectory();

try { audiofile = File.createTempFile(“sound”, “.3gp”, sampleDir); } catch (Exception e) { // TODO: handle exception Log.e(“Path Error”, “Error in making directory”); } mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mRecorder.setOutputFile(audiofile.getAbsolutePath()); try{ mRecorder.prepare(); mRecorder.start(); }catch(Exception e){ Log.e(“LOG_TAG”, “startRecording() failed”); }

}

//Code for stop recordinh
private void stopRecording(){ if(null != mRecorder){ mRecorder.stop(); mRecorder.release(); mRecorder = null; addRecordingToMediaLibrary(); }

}

//Code for adding recorded file in sdcard
private void addRecordingToMediaLibrary(){ ContentValues values = new ContentValues(4); long current = System.currentTimeMillis(); values.put(MediaStore.Audio.Media.TITLE, “audio” + audiofile.getName()); values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000)); values.put(MediaStore.Audio.Media.MIME_TYPE, “audio/3gpp”); values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath()); soundFilePath = audiofile.getAbsolutePath();

ContentResolver contentResolver = getContentResolver();

Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
Toast.makeText(this, “Added File ” + newUri, Toast.LENGTH_LONG).show();

}

785 total views, 2 views today

Share this Onfacebook-2944591twitter-2295512linkedin-3546921google-8071807