gpt4 book ai didi

android - 无法打开.mp3录音java.io.FileNotFoundException

转载 作者:行者123 更新时间:2023-12-03 02:35:25 25 4
gpt4 key购买 nike

我的项目中有一个功能,该功能允许用户记录和播放本地存储的音频备忘录。如果我为文件设置了静态名称,则一切正常。如果我尝试设置动态名称(例如System.currentTimeMillis()),则在尝试播放该文件时找不到该文件。这是我的代码:

录音位:

    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!!!");


}

并设置文件名(静态名称):
    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]);
}

这是我调试时得到的路径:
/storage/emulated/0/everreach_audio_notes//12345.mp3

和非动态选项似乎不起作用:
    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]);
}

这给了我以下路径:
/storage/emulated/0/everreach_audio_notes//1476878040697.mp3

当我尝试使用动态名称播放音频文件时,出现以下错误:
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)

最佳答案

更换

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

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

关于android - 无法打开.mp3录音java.io.FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40132978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com