λšλ”±κ°œλ°œ πŸ”¨

[React] getUserMedia() 였λ₯˜ (μ‚¬μš©μž μ˜€λ””μ˜€, λΉ„λ””μ˜€ 닀루기) λ³Έλ¬Έ

개발/🌌 React.js

[React] getUserMedia() 였λ₯˜ (μ‚¬μš©μž μ˜€λ””μ˜€, λΉ„λ””μ˜€ 닀루기)

λšλ”±κ°œλ°œμž πŸ”§ 2023. 11. 8. 10:21
λ°˜μ‘ν˜•

getUserMedia()

μ‚¬μš©μžμ—κ²Œ λ―Έλ””μ–΄ μž…λ ₯을 μ‚¬μš©ν•  수 μžˆλŠ” κΆŒν•œμ„ μ‚¬μš©μžμ—κ²Œ μš”μ²­ν•œλ‹€.  (μ˜€λ””μ˜€, λΉ„λ””μ˜€)
navigator.mediaDevices
.getUserMedia(constraints)
.then((stream) => {
/* use the stream */
})
.catch((err) => {
/* handle the error */
});

getUserMedia()의 일반적인 였λ₯˜ 

μƒμœ„ 5개 였λ₯˜

firefox chrome
AbortError
AbortError
NotAllowedError PermissionDeniedError
NotFoundError DevicesNotFoundError
NotReadableError TrackStartError
OverconstrainedError ConstraintNotSatisfiedError

 

1. AbortError

- μ‚¬μš©μžμ™€ 운영 체제 λͺ¨λ‘ ν•˜λ“œμ›¨μ–΄ μž₯μΉ˜μ— λŒ€ν•œ μ•‘μ„ΈμŠ€ κΆŒν•œμ„ λΆ€μ—¬ν–ˆμœΌλ©°, μž₯치λ₯Ό μ‚¬μš©ν•  수 μ—†κ²Œ λ§Œλ“œλŠ”NotReadableError 였λ₯˜λ₯Ό λ°œμƒμ‹œν‚€λŠ” ν•˜λ“œμ›¨μ–΄ λ¬Έμ œκ°€ λ°œμƒν•˜μ§€ μ•Šμ€ 경우

2. NotAllowedError : ν—ˆμš©λ˜μ§€ μ•ŠμŒ

NotAllowError

- message : The request is not allowed by the user agent or the platform in the current context 

- μ‚¬μš©μžκ°€ μ›ΉμΊ μ΄λ‚˜ λ§ˆμ΄ν¬μ— λŒ€ν•œ κΆŒν•œμ„ κ±°λΆ€ν•˜κ±°λ‚˜ 이전에 κ±°λΆ€ν•œ 경우

- νŽ˜μ΄μ§€κ°€ HTTPSκ°€ μ•„λ‹Œ HTTPλ₯Ό μ‚¬μš©ν•˜μ—¬ λ‘œλ“œν•œ 경우

3. NotFoundError : μž₯치 찾을 수 μ—†μŒ

- λΉ„λ””μ˜€λ₯Ό μš”μ²­ν•œ 경우 μ‚¬μš©μžμ—κ²Œ 웹캠이 μ—†λŠ” 경우

- μ˜€λ””μ˜€λ₯Ό μš”μ²­ν•œ 경우 μ‚¬μš©μžμ—κ²Œ λ§ˆμ΄ν¬κ°€ μ—†λŠ” 경우

- μ»΄ν“¨ν„°λ‚˜ μž₯μΉ˜μ— μ‚¬μš΄λ“œ μΉ΄λ“œκ°€ μ—†λŠ” 경우

- μ»΄ν“¨ν„°λ‚˜ μž₯μΉ˜μ— μ‚¬μš΄λ“œ μΉ΄λ“œκ°€ μžˆμ§€λ§Œ μž₯μΉ˜κ°€ μ—°κ²°λ˜μ–΄ μžˆμ§€ μ•Šμ€ 경우

4. NotReadableError : 읽을 수 μ—†μŒ

- μ‚¬μš©μžκ°€ μž₯치λ₯Ό μ‚¬μš©ν•  수 μžˆλŠ” κΆŒν•œμ„ λΆ€μ—¬ν–ˆμ§€λ§Œ 운영 체제, λΈŒλΌμš°μ € λ˜λŠ” μ›Ή νŽ˜μ΄μ§€ μˆ˜μ€€μ—μ„œ ν•˜λ“œμ›¨μ–΄ 였λ₯˜κ°€ λ°œμƒν•˜μ—¬ μž₯μΉ˜μ— λŒ€ν•œ κΆŒν•œμ„ μ°¨λ‹¨ν•˜λŠ” 경우

- μ»΄ν“¨ν„°μ˜ λ‹€λ₯Έ μ•±μ—μ„œ 이미 μ‚¬μš© 쀑인 경우

- μž₯μΉ˜κ°€ 이미 λ‹€λ₯Έ firefox νƒ­μ—μ„œ μ‚¬μš© 쀑인 경우(firefox만 ν•΄λ‹Ή)

- μ‚¬μš©μžκ°€ κ°œμΈμ •λ³΄ 보호 λŒ€ν™”μƒμžλ₯Ό 닫은 경우(chrome만 ν•΄λ‹Ή)

5. OverconstrainedError : μ œμ•½ 쑰건에 μΆ©μ‘±ν•˜λŠ” μž₯μΉ˜κ°€ μ—†μŒ

- message : Constraints could be not satisfied.

- constraint : λ¬Έμžμ—΄ κ°’μœΌλ‘œ μΆ©μ‘±ν•  수 μ—†λŠ” μ œμ•½ 쑰건의 이름(속성)을 λ°˜ν™˜

- μ§€μ •λœ μ œμ•½ 쑰건에 기쀀을 μΆ©μ‘±ν•˜λŠ” 후보 μž₯μΉ˜κ°€ μ—†λŠ” 경우- ex) 1920 * 1080의 μ •ν™•ν•œ 해상도λ₯Ό μš”μ²­ν• λ•Œ μ½˜μ†”μ—μ„œ error 속성쀑 constraintName : widthκ°€ ν™•μΈλœλ‹€.

 

getUserMedia() μ˜ˆμ™Έ 처리

navigator.mediaDevices
.getUserMedia({
    audio : true,
    video : true
}).then((stream) => {
    /* use the stream */
}).catch((err) => {
    console.log(err.name); 
    console.log(err.message);
    
    /* handle the error */
    if (err.name == "NotFoundError" || err.name == "DevicesNotFoundError") {
        
    } else if (err.name == "NotReadableError" || err.name == "TrackStartError") {
        
    } else if (err.name == "OverconstrainedError" || err.name == "ConstraintNotSatisfiedError") {
        
    } else if (err.name == "NotAllowedError" || err.name == "PermissionDeniedError") {
       
    } else if (err.name == "AbortError") {
        
    } else {
        
    }
});

 

λ°˜μ‘ν˜•