admin管理员组文章数量:1426804
I have created a React ponent with a microphone button that:
OnMouseDown
=> User begins recording audioOnMouseUp
=> 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
=>
- Make sure user has given permission to access microphone
- 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 audioOnMouseUp
=> 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
=>
- Make sure user has given permission to access microphone
- 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.
1 Answer
Reset to default 4In 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:
- Checks the microphone permissions against the global variable
- 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.
本文标签: javascriptEnsuring user permits Microphone access before firing event in React appStack Overflow
版权声明:本文标题:javascript - Ensuring user permits Microphone access before firing event in React app - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745446447a2658684.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论