react-native-twilio-video-webrtc

react-native-twilio-video-webrtc

React Native集成Twilio Video WebRTC功能库

react-native-twilio-video-webrtc是一个用于在React Native应用中集成Twilio Video WebRTC功能的库。支持iOS和Android平台,提供API用于连接视频房间、管理参与者和音视频轨道。主要组件包括TwilioVideo、TwilioVideoLocalView和TwilioVideoParticipantView。库提供了详细的安装指南和使用示例,方便开发者快速实现视频通话功能。

React NativeTwilio VideoWebRTC视频通话跨平台开发Github开源项目

GitHub Logo

Twilio Video (WebRTC) for React Native

Platforms:

  • iOS
  • Android

People using a version < 1.0.1 please move to 1.0.1 since the project changed a lot internally to support the stable TwilioVideo version.

Installation

  • react-native >= 0.40.0: install react-native-twilio-video-webrtc@1.0.1
  • react-native < 0.40.0: install react-native-twilio-video-webrtc@1.0.0

Install Node Package

Option A: yarn

yarn add https://github.com/blackuy/react-native-twilio-video-webrtc

Option B: npm

npm install https://github.com/blackuy/react-native-twilio-video-webrtc --save

Usage with Expo

To use this library with Expo we recommend using our config plugin that you can configure like the following example:

{ "name": "my app", "plugins": [ [ "react-native-twilio-video-webrtc", { "cameraPermission": "Allow $(PRODUCT_NAME) to access your camera", "microphonePermission": "Allow $(PRODUCT_NAME) to access your microphone" } ] ] }

Also you will need to install expo-build-properties package:

npx expo install expo-build-properties

Expo Config Plugin Props

The plugin support the following properties:

  • cameraPermission: Specifies the text to show when requesting the camera permission to the user.

  • microphonePermission: Specifies the text to show when requesting the microphone permission to the user.

iOS

Option A: Install with CocoaPods (recommended)

  1. Add this package to your Podfile
pod 'react-native-twilio-video-webrtc', path: '../node_modules/react-native-twilio-video-webrtc'

Note that this will automatically pull in the appropriate version of the underlying TwilioVideo pod.

  1. Install Pods with
pod install

Option B: Install without CocoaPods (manual approach)

  1. Add the Twilio dependency to your Podfile
pod 'TwilioVideo'
  1. Install Pods with
pod install
  1. Add the XCode project to your own XCode project's Libraries directory from
node_modules/react-native-twilio-video-webrtc/ios/RNTwilioVideoWebRTC.xcodeproj
  1. Add libRNTwilioVideoWebRTC.a to your XCode project target's Linked Frameworks and Libraries

  2. Update Build Settings

Find Search Paths and add $(SRCROOT)/../node_modules/react-native-twilio-video-webrtc/ios with recursive to Framework Search Paths and Library Search Paths

Post install

Be sure to increment your iOS Deployment Target to at least iOS 11 through XCode and your Podfile contains

platform :ios, '11.0'

Permissions

To enable camera usage and microphone usage you will need to add the following entries to your Info.plist file:

<key>NSCameraUsageDescription</key>
<string>Your message to user when the camera is accessed for the first time</string>
<key>NSMicrophoneUsageDescription</key>
<string>Your message to user when the microphone is accessed for the first time</string>

Known Issues

TwilioVideo version 1.3.8 (latest) has the following know issues.

  • Participant disconnect event can take up to 120 seconds to occur. Issue 99
  • AVPlayer audio content does not mix properly with Room audio. Issue 62

Android

As with iOS, make sure the package is installed:

yarn add https://github.com/blackuy/react-native-twilio-video-webrtc

Then add the library to your settings.gradle file:

include ':react-native-twilio-video-webrtc'
project(':react-native-twilio-video-webrtc').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-twilio-video-webrtc/android')

And include the library in your dependencies in android/app/build.gradle: (if using gradle 4 or lower, replace implementation with compile below)

dependencies {
    .....
    .....
    .....
    implementation project(':react-native-twilio-video-webrtc')
}

You will also need to update this file so that you compile with java 8 features:

android {
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
}

Now you're ready to load the package in MainApplication.java. In the imports section, add this:

import com.twiliorn.library.TwilioPackage;

Then update the getPackages() method:

protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( ... new TwilioPackage() ); }

Permissions

For most applications, you'll want to add camera and audio permissions to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-feature android:name="android.hardware.camera" android:required="false" /> <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" /> <uses-feature android:name="android.hardware.microphone" android:required="false" />

Newer versions of Android have a different permissions model. You will need to use the PermissionsAndroid class in react-native in order to request the CAMERA and RECORD_AUDIO permissions.

Additional Tips

Under default settings, the Android build will fail if the total number of symbols exceeds a certain threshold. If you should encounter this issue when adding this library (e.g., if your build fails with com.android.dex.DexIndexOverflowException), you can turn on jumbo mode by editing your app/build.gradle:

android {
  ...
  dexOptions {
    jumboMode true
  }
}

If you are using proguard (very likely), you will also need to ensure that the symbols needed by this library are not stripped. To do that, add these two lines to proguard-rules.pro:

  -keep class com.twilio.** { *; }
  -keep class tvi.webrtc.** { *; }

Docs

You can see the documentation here.

Usage

We have three important components to understand:

import { TwilioVideo, TwilioVideoLocalView, TwilioVideoParticipantView, } from "react-native-twilio-video-webrtc";
  • TwilioVideo / is responsible for connecting to rooms, events delivery and camera/audio.
  • TwilioVideoLocalView / is responsible local camera feed view
  • TwilioVideoParticipantView / is responsible remote peer's camera feed view

Here you can see a complete example of a simple application that uses almost all the apis:

import React, { Component, useRef } from "react"; import { TwilioVideoLocalView, TwilioVideoParticipantView, TwilioVideo, } from "react-native-twilio-video-webrtc"; const Example = (props) => { const [isAudioEnabled, setIsAudioEnabled] = useState(true); const [isVideoEnabled, setIsVideoEnabled] = useState(true); const [status, setStatus] = useState("disconnected"); const [participants, setParticipants] = useState(new Map()); const [videoTracks, setVideoTracks] = useState(new Map()); const [token, setToken] = useState(""); const twilioRef = useRef(null); const _onConnectButtonPress = () => { twilioRef.current.connect({ accessToken: token }); setStatus("connecting"); }; const _onEndButtonPress = () => { twilioRef.current.disconnect(); }; const _onMuteButtonPress = () => { twilioRef.current .setLocalAudioEnabled(!isAudioEnabled) .then((isEnabled) => setIsAudioEnabled(isEnabled)); }; const _onFlipButtonPress = () => { twilioRef.current.flipCamera(); }; const _onRoomDidConnect = ({ roomName, error }) => { console.log("onRoomDidConnect: ", roomName); setStatus("connected"); }; const _onRoomDidDisconnect = ({ roomName, error }) => { console.log("[Disconnect]ERROR: ", error); setStatus("disconnected"); }; const _onRoomDidFailToConnect = (error) => { console.log("[FailToConnect]ERROR: ", error); setStatus("disconnected"); }; const _onParticipantAddedVideoTrack = ({ participant, track }) => { console.log("onParticipantAddedVideoTrack: ", participant, track); setVideoTracks((originalVideoTracks) => { originalVideoTracks.set(track.trackSid, { participantSid: participant.sid, videoTrackSid: track.trackSid, }); return new Map(originalVideoTracks); }); }; const _onParticipantRemovedVideoTrack = ({ participant, track }) => { console.log("onParticipantRemovedVideoTrack: ", participant, track); setVideoTracks((originalVideoTracks) => { originalVideoTracks.delete(track.trackSid); return new Map(originalVideoTracks); }); }; return ( <View style={styles.container}> {status === "disconnected" && ( <View> <Text style={styles.welcome}>React Native Twilio Video</Text> <TextInput style={styles.input} autoCapitalize="none" value={token} onChangeText={(text) => setToken(text)} ></TextInput> <Button title="Connect" style={styles.button} onPress={_onConnectButtonPress} ></Button> </View> )} {(status === "connected" || status === "connecting") && ( <View style={styles.callContainer}> {status === "connected" && ( <View style={styles.remoteGrid}> {Array.from(videoTracks, ([trackSid, trackIdentifier]) => { return ( <TwilioVideoParticipantView style={styles.remoteVideo} key={trackSid} trackIdentifier={trackIdentifier} /> ); })} </View> )} <View style={styles.optionsContainer}> <TouchableOpacity style={styles.optionButton} onPress={_onEndButtonPress} > <Text style={{ fontSize: 12 }}>End</Text> </TouchableOpacity> <TouchableOpacity style={styles.optionButton} onPress={_onMuteButtonPress} > <Text style={{ fontSize: 12 }}> {isAudioEnabled ? "Mute" : "Unmute"} </Text> </TouchableOpacity> <TouchableOpacity style={styles.optionButton} onPress={_onFlipButtonPress} > <Text style={{ fontSize: 12 }}>Flip</Text> </TouchableOpacity> <TwilioVideoLocalView enabled={true} style={styles.localVideo} /> </View> </View> )} <TwilioVideo ref={twilioRef} onRoomDidConnect={_onRoomDidConnect} onRoomDidDisconnect={_onRoomDidDisconnect} onRoomDidFailToConnect={_onRoomDidFailToConnect} onParticipantAddedVideoTrack={_onParticipantAddedVideoTrack} onParticipantRemovedVideoTrack={_onParticipantRemovedVideoTrack} /> </View> ); }; AppRegistry.registerComponent("Example", () => Example);

Run the Example Application

To run the example application:

  • Move to the Example directory: cd Example
  • Install node dependencies: yarn install
  • Install objective-c dependencies: cd ios && pod install
  • Open the xcworkspace and run the app: open Example.xcworkspace

Migrating from 1.x to 2.x

  • Make sure your pod dependencies are updated. If you manually specified a pod version, you'll want to update it as follows:
  s.dependency 'TwilioVideo', '~> 2.2.0'
  • Both participants and tracks are uniquely identified by their sid/trackSid field. The trackId field no longer exists and should be replaced by trackSid. Commensurate with this change, participant views now expect participantSid and videoTrackSid keys in the trackIdentity prop (instead of identity and trackId).

  • Make sure you're listening to participant events via onParticipant{Added/Removed}VideoTrack rather than onParticipant{Enabled/Disabled}Track.

Contact

编辑推荐精选

Trae

Trae

字节跳动发布的AI编程神器IDE

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

热门AI工具生产力协作转型TraeAI IDE
蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI助手AI工具AI写作工具AI辅助写作蛙蛙写作学术助手办公助手营销助手
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

聊天机器人AI助手热门AI工具AI对话
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

热门AI工具AI办公办公工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

模型训练热门AI工具内容创作智能问答AI开发讯飞星火大模型多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

AI助手热门AI工具AI创作AI辅助写作讯飞绘文内容运营个性化文章多平台分发
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

下拉加载更多