当前位置:   article > 正文

React Hooks 通过js获取电脑麦克风、扬声器等设备列表,检测设备改变执行回调_js 实时获取麦克风

js 实时获取麦克风

 

可以获取到当前麦克风权限,系统和外部连接的麦克风、扬声器设备对象,检测麦克风设备改变

  1. import React, { useEffect, useMemo, useState } from "react";
  2. type Props = {
  3. onUpdated?: (devices: MediaDeviceInfo[]) => void;
  4. };
  5. enum PermissionState {
  6. GRANTED = "granted",
  7. DENIED = "denied",
  8. PROMPT = "prompt",
  9. }
  10. interface DevicesListData {
  11. devices: MediaDeviceInfo[]; //所有设备信息
  12. currentAudioInputDevice: MediaDeviceInfo; //当前默认麦克风
  13. currentAudioOutputDevice: MediaDeviceInfo; //当前默认扬声器
  14. videoInputs: MediaDeviceInfo[]; //摄像头设备
  15. userAudioInputs: MediaDeviceInfo[]; //系统和外部连接的麦克风设备
  16. userAudioOutputs: MediaDeviceInfo[]; //系统和外部连接的扬声器设备
  17. isSupported: boolean; //是否支持
  18. microphonePermission: PermissionState; //麦克风权限 granted ,denied,error
  19. }
  20. /**
  21. *
  22. * @description 获取设备列表,包括摄像头,麦克风,扬声器;并且可以获取当前默认设备信息
  23. * @param props {onUpdated} 当设备列表更新时触发
  24. * @returns DevicesListData
  25. * @example
  26. * const {
  27. * userAudioInputs,
  28. * userAudioOutputs,
  29. * currentAudioInputDevice,
  30. * currentAudioOutputDevice,
  31. * } = useDevicesList({});
  32. */
  33. export function useDevicesList(props: Props): DevicesListData {
  34. const navigator = window.navigator;
  35. const [devices, setDevices] = useState<MediaDeviceInfo[]>([]);
  36. const [microphonePermission, setMicrophonePermission] = useState(
  37. PermissionState.DENIED
  38. );
  39. const videoInputs = useMemo(
  40. () => devices.filter((i) => i.kind === "videoinput"),
  41. [devices]
  42. );
  43. const audioInputs = useMemo(
  44. () => devices.filter((i) => i.kind === "audioinput"),
  45. [devices]
  46. );
  47. const audioOutputs = useMemo(
  48. () => devices.filter((i) => i.kind === "audiooutput"),
  49. [devices]
  50. );
  51. const userAudioInputs = useMemo(
  52. () =>
  53. audioInputs.filter(
  54. (i) => i.deviceId !== "default" && i.deviceId !== "communications"
  55. ),
  56. [audioInputs]
  57. );
  58. const userAudioOutputs = useMemo(
  59. () =>
  60. audioOutputs.filter(
  61. (i) => i.deviceId !== "default" && i.deviceId !== "communications"
  62. ),
  63. [audioOutputs]
  64. );
  65. const currentAudioInputDevice = useMemo(
  66. () => audioInputs.find((i) => i.deviceId === "default"),
  67. [audioInputs]
  68. ) as MediaDeviceInfo;
  69. const currentAudioOutputDevice = useMemo(
  70. () => audioOutputs.find((i) => i.deviceId === "default"),
  71. [audioOutputs]
  72. ) as MediaDeviceInfo;
  73. const isSupported = useMemo(
  74. () =>
  75. Boolean(
  76. navigator &&
  77. navigator.mediaDevices &&
  78. navigator.mediaDevices.enumerateDevices
  79. ),
  80. [navigator]
  81. );
  82. async function update() {
  83. if (!isSupported) return;
  84. const _devices = await navigator!.mediaDevices.enumerateDevices();
  85. setDevices(_devices);
  86. props.onUpdated && props.onUpdated(_devices);
  87. }
  88. // 权限检测
  89. async function checkMicrophonePermission() {
  90. try {
  91. const stream = await navigator.mediaDevices.getUserMedia({
  92. audio: true,
  93. });
  94. // 如果获取媒体流成功,表示用户已经授予麦克风权限
  95. setMicrophonePermission(PermissionState.GRANTED);
  96. // 关闭获取媒体流的轨道
  97. const audioTracks = stream.getAudioTracks();
  98. audioTracks.forEach((track) => track.stop());
  99. } catch (error: any) {
  100. // 如果获取媒体流失败,可能是因为用户拒绝了权限请求
  101. if (
  102. error.name === "NotAllowedError" ||
  103. error.name === "PermissionDeniedError"
  104. ) {
  105. setMicrophonePermission(PermissionState.DENIED);
  106. } else {
  107. setMicrophonePermission(PermissionState.PROMPT);
  108. }
  109. }
  110. }
  111. useEffect(() => {
  112. isSupported && update();
  113. navigator!.mediaDevices.addEventListener("devicechange", update);
  114. return () => {
  115. navigator!.mediaDevices.removeEventListener("devicechange", update);
  116. };
  117. }, [navigator]);
  118. useEffect(() => {
  119. checkMicrophonePermission();
  120. }, [navigator, devices]);
  121. return {
  122. devices,
  123. videoInputs,
  124. isSupported,
  125. microphonePermission,
  126. userAudioInputs,
  127. userAudioOutputs,
  128. currentAudioInputDevice,
  129. currentAudioOutputDevice,
  130. };
  131. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/321036
推荐阅读
相关标签
  

闽ICP备14008679号