Can not open .mp3 audio recording java.io.FileNotFoundException

时间:2016-10-19 13:36:58

标签: android file audio mobile android-mediaplayer

I have a feature in my project which allows the user to record and play a audio memo which is stored locally. If I set a static name to the file, everything works fine. If I'm trying to set a dynamic name (ex: System.currentTimeMillis()) the file is not found when I'm trying to play it. Here is my code:

The recording bit:

    public void startRecording() {

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(output_formats[currentFormat]);
    // recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(getFileRecording());
    recorder.setOnErrorListener(errorListener);
    recorder.setOnInfoListener(infoListener);

    try {
        recorder.prepare();
        recorder.start();
        isRecording = true;
        showRecordingUI();

        handler.post(updateVisualizer);

    } catch (IllegalStateException e) {
        Log.e("Recording ::::: ", e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        Log.e("Recording :: ", e.getMessage());
        e.printStackTrace();
    }

    BuildDev.logDebug("WE ARE RECORDING!!!");


}

and settings the name of the file (static name):

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

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

    String fileName = "12345"

    return (file.getAbsolutePath() + "/" + fileName + file_exts[currentFormat]);
}

this is the path that I get when I debug:

/storage/emulated/0/everreach_audio_notes//12345.mp3

and the non dynamic option which doesn't seem to work:

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

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

    String fileName =  String.valueOf(System.currentTimeMillis();

    return (file.getAbsolutePath() + "/" + "/" + fileName + file_exts[currentFormat]);
}

which give me the following path:

/storage/emulated/0/everreach_audio_notes//1476878040697.mp3

When I'm trying to play the audio file with the dynamic name, I get this error:

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/everreach_audio_notes/11476880948244.mp3: open failed: ENOENT (No such file or directory)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at java.io.FileInputStream.<init>(FileInputStream.java:103)
call_info.RecordingsListAdapter.startPlaying(RecordingsListAdapter.java:190)
call_info.RecordingsListAdapter.access$500(RecordingsListAdapter.java:36)
call_info.RecordingsListAdapter$2.onClick(RecordingsListAdapter.java:125)
at android.view.View.performClick(View.java:4861)
at android.view.View$PerformClick.run(View.java:19980)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:451)

2 个答案:

答案 0 :(得分:1)

Replace

  return (file.getAbsolutePath() + "/" + "/" + fileName + file_exts[currentFormat]);

by

  return (file.getAbsolutePath() + "/" + fileName + file_exts[currentFormat]);

答案 1 :(得分:0)

再次检查堆栈跟踪

 java.io.FileNotFoundException:  /storage/emulated/0/everreach_audio_notes/11476880948244.mp3: open failed: ENOENT (No such file or directory)

你说你得到了

/storage/emulated/0/audio_notes//1476878040697.mp3 

来自getFileRecording()方法

/everreach_audio_notes//audio_notes/不一样