Becky Reyna
Becky Reyna

Reputation: 97

Record audio and save into app's data folder

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;

Upvotes: 1

Views: 4763

Answers (3)

Shahul3D
Shahul3D

Reputation: 2149

It is recommended to store files on your data directory. (ie. sdcard/Android/data/yourpackage/) So that the folder can be removed automatically once the user uninstalls your application.

You can use the below code to get your data folder: (if your Build.VERSION.SDK_INT is less than Build.VERSION_CODES.FROYO )

Environment.getExternalStorageDirectory() +"/Android/data/" +context.getApplicationContext().getPackageName());

(if your Build.VERSION.SDK_INT is greater than Build.VERSION_CODES.FROYO )

context.getExternalFilesDir(Context.STORAGE_SERVICE)

Upvotes: 0

Annabel
Annabel

Reputation: 430

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

Upvotes: 4

NaviRamyle
NaviRamyle

Reputation: 4007

You need to add this uses-permission in your manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Use this for the path to sd card

File sdcard = Environment.getExternalStorageDirectory();
File storagePath = new File(sdcard.getAbsolutePath() + "/folderName");
File userimage = new File(storagePath + "/" + fileName + ".3gpp");

Upvotes: 1

Related Questions