I am quite new to programming and I would like to learn how to do an app that allows user to record the audio and hence, save it into the app's data folder. I managed to do the recording part but only managed to save it into the sd card.. Anybody willing to help me figure out how to save my audio into an internal storage??
private void playRecording() throws Exception {
ditchMediaPlayer();
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(OUTPUT_FILE);
mediaPlayer.prepare();
mediaPlayer.start();
}
private void stopRecording() {
if(recorder != null)
recorder.stop();
}
private void beginRecording() throws Exception {
ditchMediaRecorder();
File outFile = new File(OUTPUT_FILE);
if(outFile.exists())
outFile.delete();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(OUTPUT_FILE);
recorder.prepare();
recorder.start();
}
private void ditchMediaRecorder() {
if (recorder != null)
recorder.release();
}
public void recordOnClick(View v) {
//when record button is pressed
try{
beginRecording();
}catch (Exception e){
e.printStackTrace();
}
btnRecord.setVisibility(View.INVISIBLE);
btnStop.setVisibility(View.VISIBLE);
}
public void stopOnClick(View v) {
//when stop button is pressed
try{
stopRecording();
}
catch (Exception e){
e.printStackTrace();
}
btnStop.setVisibility(View.INVISIBLE);
btnRecord.setVisibility(View.INVISIBLE);
btnDelete.setVisibility(View.VISIBLE);
btnPlay.setVisibility(View.VISIBLE);
btnShare.setVisibility(View.VISIBLE);
}
public void playOnClick(View v) {
//when play button is pressed
try{
playRecording();
}
catch (Exception e){
e.printStackTrace();
}
btnStop.setVisibility(View.INVISIBLE);
btnRecord.setVisibility(View.INVISIBLE);
}
And this is my onClick where I will specify my output per button (im doing a chart so I have around 44 buttons!)
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
OUTPUT_FILE=Environment.getExternalStorageDirectory()+"/y1.3gpp";
File file1 = new File(Environment.getExternalStorageDirectory()+"/y1.3gpp");
if (file1.exists()) {
btnPlay.setVisibility(View.VISIBLE);
btnDelete.setVisibility(View.VISIBLE);
btnShare.setVisibility(View.VISIBLE);
}
else {
btnRecord.setVisibility(View.VISIBLE);
}
break;
case R.id.btn2:
OUTPUT_FILE=Environment.getExternalStorageDirectory()+"/y2.3gpp";
File file2 = new File(Environment.getExternalStorageDirectory()+ "/y2.3gpp" );
if (file2.exists()) {
btnPlay.setVisibility(View.VISIBLE);
btnDelete.setVisibility(View.VISIBLE);
btnShare.setVisibility(View.VISIBLE);
}
else {
btnRecord.setVisibility(View.VISIBLE);
}
break;
maybe you would want to take a look at this? Creating folder in internal Memory to save files and retrieve them later
//Creating an internal directory
File mydir = context.getDir("mydir", Context.MODE_PRIVATE);
//Getting a file within the dir.
File fileWithinMyDir = new File(mydir, "myfile");
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
//Use the stream as usual to write into the file