简体   繁体   中英

Recorded Audio Can't be saved in /storage/emulated/0/AudioRecorder (sometimes saved file appear, sometimes not appear)

I want to save my recorded audio at /storage/emulated/0/AudioRecorder, This is my code :

private String getFilename() {
        File file = new File(Environment.getExternalStorageDirectory(), "/AudioRecorder");
        if (!file.exists()) {
            file.mkdirs();
        }

        createdDate = DateTools.getDate(System.currentTimeMillis(),"dd-MMM-yyyy hh:mm:ss");
        nameFile = createdDate + ".mp4";

        pathFile = (file.getAbsolutePath() + "/" + nameFile);
        return pathFile;
    }

private void startRecording() {
        if(RadioManager.getInstance().isPlaying()){
            RadioManager.getInstance().stopPlayer();
        }
        Toast.makeText(VoiceNoteActivity.this, "Mulai Rekam", Toast.LENGTH_SHORT).show();
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(output_formats);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(getFilename());
        recorder.setOnErrorListener(errorListener);
        recorder.setOnInfoListener(infoListener);
        try {
            recorder.prepare();
            recorder.start();
            isRecord = true;
            countDown.start();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void stopRecording() {
        if (null != recorder) {
            countDown.cancel();

            timer = 0;
            seekbar.setProgress(timer);
            try{
                recorder.stop();
                recorder.reset();
                recorder.release();
                recorder = null;
            }catch(RuntimeException stopException){
                //handle cleanup here
                Log.e("Voice Note Activity ","Voice Recording Error : " +stopException.getMessage());
            }

            isRecord = false;
            time.setText("00:00");
            enableButtons(false);
        }
    }

The file sometimes appear in /storage/emulated/0/AudioRecorder, but sometimes not appear (more often to not appear). Anybody can help ?

Hem.. I got same problem but with picture not voice. In my case the picture didn't appear because I haven't throw it to gallery. Based on the tutorial on android developer website is it the code for throw picture to gallery:

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
    File f = new File(fileUri.getPath());
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

may be you can use it to voice file. but I haven't try to voice file.

I have solved this by adding this code :

File file = new File(getFilename());
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

So, when the file is exist, it doesn't need to create that file again, but when saved file not exist, it needs to create the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM