Last active
October 25, 2022 16:18
-
-
Save ozcanzaferayan/73ab1e39dee29646691252ff983e16f4 to your computer and use it in GitHub Desktop.
React Native WebRTC localStream example
This file contains hidden or 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
import React, {useState} from 'react'; | |
import {SafeAreaView, StyleSheet, View, Button} from 'react-native'; | |
import { | |
RTCView, | |
mediaDevices, | |
MediaStream, | |
MediaStreamConstraints, | |
} from 'react-native-webrtc'; | |
const App = () => { | |
const [localStream, setLocalStream] = useState<MediaStream>(); | |
const startLocalStream = async () => { | |
const isFrontCamera = true; | |
const devices = await mediaDevices.enumerateDevices(); | |
const facing = isFrontCamera ? 'front' : 'environment'; | |
const videoSourceId = devices.find( | |
(device: any) => device.kind === 'videoinput' && device.facing === facing, | |
); | |
const facingMode = isFrontCamera ? 'user' : 'environment'; | |
const constraints: MediaStreamConstraints = { | |
audio: true, | |
video: { | |
mandatory: { | |
minWidth: 500, | |
minHeight: 300, | |
minFrameRate: 30, | |
}, | |
facingMode, | |
optional: videoSourceId ? [{sourceId: videoSourceId}] : [], | |
}, | |
}; | |
const newStream: any = await mediaDevices.getUserMedia(constraints); | |
setLocalStream(newStream); | |
}; | |
return ( | |
<SafeAreaView style={styles.container}> | |
<Button title="Kamerayı aç ve akışa başla" onPress={startLocalStream} /> | |
<View style={styles.rtcview}> | |
{localStream && ( | |
<RTCView style={styles.rtc} streamURL={localStream.toURL()} /> | |
)} | |
</View> | |
</SafeAreaView> | |
); | |
}; | |
const styles = StyleSheet.create({ | |
container: { | |
backgroundColor: '#333', | |
justifyContent: 'space-between', | |
alignItems: 'center', | |
height: '100%', | |
}, | |
rtcview: { | |
justifyContent: 'center', | |
alignItems: 'center', | |
height: '100%', | |
width: '100%', | |
backgroundColor: 'black', | |
}, | |
rtc: { | |
width: '100%', | |
height: '100%', | |
}, | |
}); | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment