TJBlack31
TJBlack31

Reputation: 785

permissions dialog not displaying - Easy Permissions

I'm working on an app that is supposed to have "RECORD_AUDIO" permission, but after implementing the following code, the dialog is not showing. Whenever I manually go into the app settings on the device to switch on the permission, it works fine.

public class MainActivity extends AppCompatActivity implements  EasyPermissions.PermissionCallbacks {

AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050,1024,0);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] perms = {Manifest.permission.RECORD_AUDIO};
    if (EasyPermissions.hasPermissions(this, perms)) {
        initiateAudio();
        // Already have permission, do the thing
        // ...
    } else {
        // Do not have permissions, request them now
        EasyPermissions.requestPermissions(this, "REQUEST_AUDIO",
                1, perms);
    }



}


private void initiateAudio(){

    PitchDetectionHandler pdh = new PitchDetectionHandler() {
        @Override
        public void handlePitch(PitchDetectionResult result, AudioEvent e) {
            final float pitchInHz = result.getPitch();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    TextView text = (TextView) findViewById(R.id.textView);
                    text.setText("" + pitchInHz);
                }
            });
        }
    };

    AudioProcessor p = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pdh);
    dispatcher.addAudioProcessor(p);
    new Thread(dispatcher,"Audio Dispatcher").start();

}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);

}

@Override
public void onPermissionsGranted(int requestCode, List<String> list) {
    initiateAudio();
}

@Override
public void onPermissionsDenied(int requestCode, List<String> list) {
    Toast.makeText(this, "Audio Permission not granted", Toast.LENGTH_LONG).show();
}
}

And here is the relevant portion of the manifest:

 <uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="26" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>

Is there something simple that I'm missing here?

Upvotes: 0

Views: 608

Answers (1)

TJBlack31
TJBlack31

Reputation: 785

I found out what the issue was:

AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050,1024,0);

needs to be called after permissions are requested, not before.

Upvotes: 1

Related Questions