How to create a sound library?

Asked

Viewed 94 times

4

In my application is recorded sound normally, only I wanted to rename the recorded sound and then store in the device.

outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/javacodegeeksRecording.3gpp";

myRecorder = new MediaRecorder();
myRecorder.setAudioSource (MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat (MediaRecorder.OutputFormat.THREE_GPP);
myRecorder.setAudioEncoder (MediaRecorder.OutputFormat.AMR_NB);
myRecorder.setOutputFile (outputFile);

 public void start(View view){ // começar a gravar
        try {
            myRecorder.prepare();
            myRecorder.start();

            //imglisten.setEnabled(false);
        } catch (IllegalStateException e) {
            // start:it is called before prepare()
            // prepare: it is called after start() or before setOutputFormat()
            e.printStackTrace();
        } catch (IOException e) {
            // prepare() fails
            e.printStackTrace();
        }
    }

    public void stop(View view){ //para de gravar
        try {
            myRecorder.stop();
            myRecorder.release();
            myRecorder  = null;

            //imglisten.setEnabled(true);

        } catch (IllegalStateException e) {
            //  it is called before start()
            e.printStackTrace();
        } catch (RuntimeException e) {
            // no valid audio/video data has been received
            e.printStackTrace();
        }

    }

I wanted it soon after to stop recording(stop), the save button could rename the recorded and stored sound. How can I do this?

1 answer

1


Your file on audio was recorded in the past path to the method myRecorder.setOutputFile();:

outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                          "/javacodegeeksRecording.3gpp";
....
....
myRecorder.setOutputFile (outputFile);

To rename this file use the method renamed() class File:

//Criar um objecto File com o caminho actual
File actualAudio = new File(outputFile);

//Caminho com o novo nome
String novoNome = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                              "/novo_nome.3gpp";

//Criar um objecto File com o caminho novo
File novoAudio = new File(novoNome);

//Fazer o rename
boolean sucesso = actualAudio.renameTo(novoAudio);
  • have 2 questions: 1 - How could I do for when the user click save, open a window and then it can rename the file and store on the device? 2 - All recorded files are already being stored on the device ? If not, how could I put to save on the device?

  • The files are soon stored. If you want to ask the user what file name do this before you start recording. Instead of outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + 
 "/javacodegeeksRecording.3gpp"; open a dialog to ask and pass the answer in the method myRecorder.setOutputFile(resposta_do_utilizador). In that case it is not necessary to do the name. Research on how to do Dialogs. If you have any difficulty post a new question.

  • I managed to do the dialog, so in this part of the new File codeAudio = new File(newName); it already replaces the old? and also did not understand this part here Boolean success = actualAudio.renameTo(newAudio);

  • The line that makes the name is boolean sucesso = actualAudio.renameTo(novoAudio);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.