nstudio / nativescript-audio

:microphone: NativeScript plugin to record and play audio :musical_note:
Other
150 stars 104 forks source link

Not checking iOS permission, TNSRecorder.CAN_RECORD() always return true on iOS #151

Closed JCFowler closed 5 years ago

JCFowler commented 5 years ago

This plugin works perfectly on Android, but iOS i'm having a few problems. When the user either denies permission to record, or accepts permission and the goes into settings and turns microphone off for the app, the app just hangs on the start() method for iOS. All I get back in the log is this:

NativeScript-Audio - TNSRecorder setCategoryError: null

It doesn't reject the promise, or finishes the promise, so it just gets hung up on the method.

Also I noticed that TNSRecorder.CAN_RECORD(): will always return true on iOS.

My question is, is there a way to check permission for iOS before the start() call? Or is there something I'm missing on the iOS side to make this a lot easier?

Thank you!

NathanWalker commented 5 years ago

@JCFowler give this a shot in your code:

switch (AVAudioSession.sharedInstance().recordPermission) {
      case AVAudioSessionRecordPermission.Granted:
        // hooray proceed with recorder
        break;
      case AVAudioSessionRecordPermission.Denied:
        // prompt user they need to allow the permission in setitngs
        break;
      case AVAudioSessionRecordPermission.Undetermined:
        // This is the initial state before a user has made any choice
        // You can use this spot to request permission here if you want
        break;
    }

Do that before interacting with the TNSRecorder 👍

JCFowler commented 5 years ago

@NathanWalker Awesome!!! This is exactly what I was looking for!! I'm surprised that all my google searching yesterday, I couldn't find a solution like this. Thanks a lot Nathan!

Just incase anyone else will find this post, In order to use AVAudioSession, you need to add the iOS reference to your project. You can follow this guide to do that: https://docs.nativescript.org/core-concepts/accessing-native-apis-with-javascript#intellisense-and-access-to-the-native-apis-via-typescript