Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save the recorded audio files in another folder programmatically?

i'm trying to save the recorded audio files in a folder that i wanted it to be rather then the default folder. but somehow i failed to do so.

my code:

Intent recordIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
Uri mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "/Record/sound_"+ String.valueOf(System.currentTimeMillis()) + ".amr"));
recordIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(recordIntent, RESULT_OK);

it did calls the voice recorder app. and also when i press the stop button, it return to my app and have a toast appeared saying its saved. but, rather then saving in my Record folder, it save in the default folder.

i realized that there is error msg in the logcat :

01-29 01:34:23.900: E/ActivityThread(10824): Activity com.sec.android.app.voicerecorder.VoiceRecorderMainActivity has leaked ServiceConnection com.sec.android.app.voicerecorder.util.VRUtil$ServiceBinder@405ce7c8 that was originally bound here

i'm not sure what went wrong as the code works when i call the camera app.

like image 585
starvi Avatar asked Dec 27 '22 07:12

starvi


1 Answers

Do in this way, Record with MediaRecorder:

To start Recording:

public  void startRecording()
        {


                MediaRecorder recorder = new MediaRecorder();

                recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                recorder.setOutputFile(getFilename());

                recorder.setOnErrorListener(errorListener);
                recorder.setOnInfoListener(infoListener);

                try 
                {
                        recorder.prepare();
                        recorder.start();
                } 
                catch (IllegalStateException e) 
                {
                        e.printStackTrace();
                } catch (IOException e) 
                {
                        e.printStackTrace();
                }
        }

To Stop:

 private void stopRecording()
    {


            if(null != recorder)
            {     
                    recorder.stop();
                    recorder.reset();
                    recorder.release();
                   recorder = null;
            }

For Selected Folder:

 private String getFilename()
        {
                String filepath = Environment.getExternalStorageDirectory().getPath();
                File file = new File(filepath,AUDIO_RECORDER_FOLDER);

                if(!file.exists()){
                        file.mkdirs();
                }

                return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
        }
like image 83
Abhi Avatar answered Feb 15 '23 22:02

Abhi