1

I'm playing and recording an audio simultaneously in order to perform audio matching analysis using musicg API. I'm getting the following error/s everytime I run my app:

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

I'm trying to save the recording to a file and then pass it to getScore() method in order to compare it to the original audio file played and return a matching score. Below is the relevant code in my MainActivity with the lines giving the error in bold.

EDIT 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);
        }
Zack
  • 377
  • 6
  • 16
  • Look at how you're creating the filename in `getFilename()`. If you call that twice, a millisecond apart, it's going to return a different filename each time. Also, `"R.raw.forrest_gump_theme"` is not going to be a valid filename. You can't access resources as though they were files. – Mike M. Apr 06 '17 at 03:11
  • @MikeM. I'm not 100% clear on the first part of your answer. I call getFilename () twice (once in getScore (), and once in stopRecording), is that the root of my problem? Also, how can I access resources if I can't directly call the file name? Can please provide more details. – Zack Apr 06 '17 at 03:16
  • You're using `System.currentTimeMillis()` in your filename. If the current time changes, so does the `String` returned by `getFilename()`. Save the value returned from the first call. Also, you can get an `InputStream` for a raw resource with `getResources().openRawResource()` called on your `Activity`, or other `Context`. – Mike M. Apr 06 '17 at 03:37
  • @MikeM. Please check my EDIT 1 in my question, I took your comments into consideration. Also, as for the resources file, would the following be correct " String fileName2 = getResources().openRawResource(R.raw.forrest_gump_theme);" ? – Zack Apr 06 '17 at 03:49
  • @MikeM. I think it would be very helpful if you can post a full answer so I can pick it as sufficient to my question. – Zack Apr 06 '17 at 03:49

1 Answers1

2

I think the problem is in copyWaveFile():

You are sending mFileName variable to copyWaveFile() but changing its path using following code:

 outFilename = Environment.getExternalStorageDirectory().getPath();

Try removing above line and check once again. Let me know if same error persists.

Sonam
  • 572
  • 4
  • 13
  • I'm getting the same error for the second file (fis2). I think I'm calling the raw audio file from the resources folder incorrectly. "String fileName2 = "R.raw.forrest_gump_theme";", what is the right way to do it? – Zack Apr 07 '17 at 04:54
  • 1
    Please check below link for raw file: http://stackoverflow.com/questions/16741433/how-to-open-a-file-in-raw-folder-in-android – Sonam Apr 07 '17 at 08:08