RN的WebRTC模块 – react-native-webrtc

weixin_38054276 2019-09-12 11:49:04
react-native-webrtc React Native的WebRTC模块。 支持 目前支持iOS 和 Android. 支持视频和音频交流. 支持数据通道 你可以使用它创建一个 iOS/Android应用,可以与web浏览器交流. 使用方法 你可以像在浏览器中一样使用WebRTC,在你的index.ios.js/index.android.js你可以require WebRTC 以导入RTCPeerConnection, RTCSessionDescription等。 var WebRTC = require('react-native-webrtc'); var { RTCPeerConnection, RTCIceCandidate, RTCSessionDescription, RTCView, MediaStream, MediaStreamTrack, getUserMedia, } = WebRTC; 使用RTCPeerConnection, RTCSessionDescription 和 RTCIceCandidate类似浏览器。 var configuration = {"iceServers": [{"url": "stun:stun.l.google.com:19302"}]}; var pc = new RTCPeerConnection(configuration); let isFront = true; MediaStreamTrack.getSources(sourceInfos => { console.log(sourceInfos); let videoSourceId; for (const i = 0; i < sourceInfos.length; i++) { const sourceInfo = sourceInfos[i]; if(sourceInfo.kind == "video" && sourceInfo.facing == (isFront ? "front" : "back")) { videoSourceId = sourceInfo.id; } } getUserMedia({ audio: true, video: { mandatory: { minWidth: 500, // Provide your own width, height and frame rate here minHeight: 300, minFrameRate: 30 }, facingMode: (isFront ? "user" : "environment"), optional: (videoSourceId ? [{sourceId: videoSourceId}] : []) } }, function (stream) { console.log('dddd', stream); callback(stream); }, logError); }); pc.createOffer(function(desc) { pc.setLocalDescription(desc, function () { // Send pc.localDescription to peer }, function(e) {}); }, function(e) {}); pc.onicecandidate = function (event) { // send event.candidate to peer }; // also support setRemoteDescription, createAnswer, addIceCandidate, onnegotiationneeded, oniceconnectionstatechange, onsignalingstatechange, onaddstream 渲染RTCView. var container; var RCTWebRTCDemo = React.createClass({ getInitialState: function() { return {videoURL: null}; }, componentDidMount: function() { container = this; }, render: function() { return ( <View> <RTCView streamURL={this.state.videoURL}/> </View> ); } }); 设置RTCView的stream container.setState({videoURL: stream.toURL()});
...全文
140 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧