我正在播放和錄製音頻,以便使用musicg API執行音頻匹配分析。我收到以下錯誤/秒,每次我跑我的應用程序:Android:沒有找到這樣的文件或目錄錯誤?
V/playRecordAudio: Playing sound & recording stopped
W/System.err: java.io.FileNotFoundException: /storage/emulated/0: open failed: EISDIR (Is a directory)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
W/System.err: at com.android.project1.main.fragments.SpeakersFragment.copyWaveFile(SpeakersFragment.java:316)
W/System.err: at com.android.project1.main.fragments.SpeakersFragment.stopRecording(SpeakersFragment.java:284)
W/System.err: at com.android.project1.main.fragments.SpeakersFragment.access$200(SpeakersFragment.java:45)
W/System.err: at com.android.project1.main.fragments.SpeakersFragment$3.onCompletion(SpeakersFragment.java:176)
W/System.err: at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2821)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/System.err: Caused by: android.system.ErrnoException: open failed: EISDIR (Is a directory)
W/System.err: at libcore.io.Posix.open(Native Method)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
W/System.err: ... 14 more
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/AudioRecorder/1491446370522.wav: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:103)
W/System.err: at com.android.project1.main.fragments.SpeakersFragment.getScore(SpeakersFragment.java:399)
W/System.err: at com.android.project1.main.fragments.SpeakersFragment.stopRecording(SpeakersFragment.java:285)
W/System.err: at com.android.project1.main.fragments.SpeakersFragment.access$200(SpeakersFragment.java:45)
W/System.err: at com.android.project1.main.fragments.SpeakersFragment$3.onCompletion(SpeakersFragment.java:176)
W/System.err: at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2821)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.Posix.open(Native Method)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
W/System.err: ... 13 more
I/System.out: 0.0
W/MediaPlayer: mediaplayer went away with unhandled events
我試圖在記錄保存到一個文件,然後把它傳遞給getScore()方法,以將其與原來的播放音頻文件並返回匹配的分數。以下是我的MainActivity中的相關代碼,其中以粗體顯示錯誤的行。
編輯1:
String mFileName = getFilename();
...
private void stopRecording() {
if (null != recorder) {
isRecording = false;
int i = recorder.getState();
if (i == 1)
recorder.stop();
recorder.release();
recorder = null;
recordingThread = null;
}
copyWaveFile(getTempFilename(), mFileName);
getScore(); //error here
deleteTempFile();
}
public void getScore(){
String fileName1 = mFileName;
String fileName2 = "R.raw.forrest_gump_theme"; //Need to change this
try {
//InputStream fis1 = null, fis2 = null;
InputStream fis1, fis2;
fis1 = new FileInputStream(fileName1);//error here
fis2 = new FileInputStream(fileName2);
Wave wave1 = new Wave(fis1), wave2 = new Wave (fis2);
FingerprintSimilarity similarity;
similarity = wave1.getFingerprintSimilarity(wave2);
result = similarity.getSimilarity()*100;
}catch (Exception e){
e.printStackTrace();
}
System.out.println(result);
}
看看你如何在'getFilename()'中創建文件名。如果你每隔一毫秒調用兩次,它將每次返回一個不同的文件名。另外,'「R.raw.forrest_gump_theme」'不會是一個有效的文件名。您無法像訪問文件一樣訪問資源。 –
@MikeM。我的答案的第一部分不是100%清楚。我調用getFilename()兩次(一次在getScore()中,一次在stopRecording中),是我的問題的根源?另外,如果我不能直接調用文件名,我該如何訪問資源?可以請提供更多細節。 – Zack
您在文件名中使用'System.currentTimeMillis()'。如果當前時間改變,getFilename()返回的'String'也會改變。保存第一次調用返回的值。另外,你可以通過getResources()。openRawResource()''''''''''''''''''''''或者'Context'來調用原始資源的'InputStream'。 –