-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from AgoraIO/dev-xuch
FIX: select camera or microphone not work
- Loading branch information
Showing
5 changed files
with
96 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
One-to-One-Video/Agora-Web-Tutorial-1to1-React/src/hooks/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as useCamera } from './useCamera'; | ||
export { default as useMicrophone } from './useMicrophone'; | ||
export { default as useMediaStream } from './useMediaStream'; | ||
export { default as usePermission } from './usePermission'; |
14 changes: 14 additions & 0 deletions
14
One-to-One-Video/Agora-Web-Tutorial-1to1-React/src/hooks/useMounted.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { useEffect, useCallback, useRef } from 'react'; | ||
|
||
const useMounted = (): (() => boolean) => { | ||
const ref = useRef<boolean>(false); | ||
useEffect(() => { | ||
ref.current = true; | ||
return () => { | ||
ref.current = false; | ||
}; | ||
}, []); | ||
return useCallback(() => ref.current, []); | ||
}; | ||
|
||
export default useMounted; |
32 changes: 32 additions & 0 deletions
32
One-to-One-Video/Agora-Web-Tutorial-1to1-React/src/hooks/usePermission.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useEffect, useState } from 'react'; | ||
import useMounted from './useMounted'; | ||
import AgoraRTC from '../utils/AgoraEnhancer'; | ||
|
||
const usePermission = () => { | ||
const [hasPermission, setHasPermission] = useState(false); | ||
const isMounted = useMounted(); | ||
|
||
// request media permissions | ||
useEffect(() => { | ||
const tempAudioStream = AgoraRTC.createStream({ audio: true, video: false }); | ||
const tempVideoStream = AgoraRTC.createStream({ audio: false, video: true }); | ||
Promise | ||
.all([tempAudioStream.init(), tempVideoStream.init()]) | ||
.then(_ => { | ||
return true; | ||
}) | ||
.catch(error => { | ||
console.log(error) | ||
return false; | ||
}) | ||
.then(has => { | ||
tempAudioStream.close(); | ||
tempVideoStream.close(); | ||
isMounted() && setHasPermission(has) | ||
}) | ||
}, []); | ||
|
||
return hasPermission; | ||
} | ||
|
||
export default usePermission; |