How to create a library for sound?

4

In my application the sound is recorded normally, except that I wanted to rename the recorded sound and then store it on 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 to stop short, the save button could rename the recorded and stored sound. How can I do this?

    
asked by anonymous 24.06.2015 / 21:53

1 answer

1

Your audio file was written in the path passed to myRecorder.setOutputFile(); method:

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

To rename this file, use the renameTo () of the File class:

//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);
    
25.06.2015 / 11:10