0

I'm using TarsosDSP for an android project, and I'm getting this error:

Unable to start activity ComponentInfo{com.example.song2sheet/com.example.song2sheet.RecordingActivity}: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.

From what I've read, to start taking samples all I need is this

        AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050, 1024, 0);
        PitchDetectionHandler detectionHandler = new PitchDetectionHandler() {
            @Override
            public void handlePitch(PitchDetectionResult pitchDetectionResult, AudioEvent audioEvent) {
                final float pitchInHz = pitchDetectionResult.getPitch();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        sampledFrequency = pitchInHz;
                    }
                });
            }
        };
        AudioProcessor pitchProcessor = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_PITCH, 22020, 1024, detectionHandler);
        dispatcher.addAudioProcessor(pitchProcessor);

        Thread recordingThread = new Thread(dispatcher, "Recording Thread");
        recordingThread.start();

but as seen above I'm getting an IllegalStateException.

Am I missing something from this code, as the error is being thrown on the first line?

Any help would be grand.

Thanks.

  • Do you have the `RECORD_AUDIO` permission granted? Not only listed in the manifest, but also requested at runtime, if you're running on Marshmallow or above? – Mike M. Apr 06 '20 at 12:32
  • I should have mentioned that you can do a quick test to check if that's the issue by manually granting that permission on your app's page in the device Settings. Eventually, though, you would need to implement that runtime permission request, if indeed you haven't yet. – Mike M. Apr 06 '20 at 12:55
  • I do now but it's still throwing the same exception. – Peter Caleb Matthew Apr 06 '20 at 13:02
  • I've not used that library at all, but that seems a little unlikely. Are you absolutely certain that it's the _exact_ same Exception and message? If you're requesting at runtime in your test, are you sure that the permission is granted before you call that code? Requesting permissions is an asynchronous operation, so your code isn't going to pause during a `requestPermissions()` call. You have to wait until `onRequestPermissionsResult()` runs, first checking that it was indeed granted. – Mike M. Apr 06 '20 at 13:09

0 Answers0