javascript - Ensuring user permits Microphone access before firing event in React app - Stack Overflow

I have created a React ponent with a microphone button that:OnMouseDown => User begins recording aud

I have created a React ponent with a microphone button that:

  • OnMouseDown => User begins recording audio
  • OnMouseUp => Audio recording ends

In other words, as long as the button is held down, the user can continue recording (similar to WhatsApp / other apps voice-message feature).

My issue is, on the first time the page appears on a user's desktop, the moment they click the button to record, Chrome pops up a dialog asking the user permission to access the microphone.

The problem with that is that, in order to click "ok" on the dialog, the user has to Mouse-up on the button which is causing an error due to the recording element not having been created yet.

Is there a way to make it such that OnMouseDown =>

  1. Make sure user has given permission to access microphone
  2. If not, ask for permission without firing off the recording sequence yet

From the research I've done, it seems I need to do something along the lines of:

const onMouseDown = async () => {
    await navigator.mediaDevices.getUserMedia({ audio: true });
    // rest of code
};

But it seems that that actually starts a recorder (and there won't be any corresponding MouseUp event to end it) and all I want to do with this portion of the code is:

  • Nothing if user has already given permission
  • Ask for permission if the first time and exit event to ensure microphone is enabled for the next time the user clicks.

I have created a React ponent with a microphone button that:

  • OnMouseDown => User begins recording audio
  • OnMouseUp => Audio recording ends

In other words, as long as the button is held down, the user can continue recording (similar to WhatsApp / other apps voice-message feature).

My issue is, on the first time the page appears on a user's desktop, the moment they click the button to record, Chrome pops up a dialog asking the user permission to access the microphone.

The problem with that is that, in order to click "ok" on the dialog, the user has to Mouse-up on the button which is causing an error due to the recording element not having been created yet.

Is there a way to make it such that OnMouseDown =>

  1. Make sure user has given permission to access microphone
  2. If not, ask for permission without firing off the recording sequence yet

From the research I've done, it seems I need to do something along the lines of:

const onMouseDown = async () => {
    await navigator.mediaDevices.getUserMedia({ audio: true });
    // rest of code
};

But it seems that that actually starts a recorder (and there won't be any corresponding MouseUp event to end it) and all I want to do with this portion of the code is:

  • Nothing if user has already given permission
  • Ask for permission if the first time and exit event to ensure microphone is enabled for the next time the user clicks.
Share Improve this question edited Sep 4, 2021 at 21:36 John Bustos asked Sep 4, 2021 at 21:17 John BustosJohn Bustos 19.6k18 gold badges100 silver badges160 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

In case someone gets caught up with the same issue, what I ended up having to do was create a global variable checking if microphone permissions were granted (I did this via Redux). Then, when the ponent first mounts, I run an action that:

  1. Checks the microphone permissions against the global variable
  2. If not granted / denied, then it runs the following:
    export const checkMicrophonePermissions = () => async dispatch => {
          let retval = false;
    
          await navigator.mediaDevices.getUserMedia({ audio: true })
            .then(stream => retval = true)
            .catch(err => retval = false);
    
          dispatch({
              type: MICROPHONE_ACCESS,
              payload: retval
          });
      }

Basically, it will run as the page loads the first time and pop-up a dialog asking the user to permit microphone access and sets the global variable relating to MICROPHONE_ACCESS to true / false based upon the user's reply.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745446448a4628068.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信