npm i --save nativescript-webrtc-plugin
- 版本:2.0.0-alpha.22
- GitHub:
- NPM: https://npmjs.net.cn/package/nativescript-webrtc-plugin
- 下载量
- 昨日:2
- 上周:14
- 上月:145
NativeScript WebRTC
安装
tns plugin add nativescript-webrtc-plugin
Android
将以下内容添加到位于 app/App_Resources
的 app.gradle 文件中
android {
....
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
如何进行通话
import { WebRTC } from 'nativescript-webrtc-plugin';
WebRTC.init(); // <= Try calling this in you app.js or app.ts or main.ts
重要:请确保在页面元素上包含 xmlns:webrtc="nativescript-webrtc-plugin"
<webrtc:WebRTCView id="remoteVideoView" height="50%" />
<webrtc:WebRTCView id="localVideoView" height="50%" />
使用 Angular 吗?
从 nativescript-webrtc-plugin/angular 导入 WebRTCModule 并将其添加到初始 @NgModule 的导入中,如此处所示。
Vue
import Vue from 'nativescript-vue';
import WebRTCView from 'nativescript-webrtc-plugin/vue';
Vue.use(WebRTCView);
<WebRTCView #remoteVideoView height="50%" ></WebRTCView>
<WebRTCView #localVideoView height="50%" ></WebRTCView>
标准API :sparkles: 新 :sparkles
此 API 与 webrtc 浏览器 API 相似 -> 示例,但以 TNS
结尾,例如 TNSRTCPeerConnection
基本API
主叫
import { WebRTC } from 'nativescript-webrtc-plugin';
const webrtc = new WebRTC({
enableAudio: true, // default true
enableVideo: false, // default true
iceServers: [
// Optional defaults to google stun servers
{
url: 'stun:stun.l.google.com:19302'
},
{
url: 'serverRequiresAuth',
username: 'username',
password: 'password'
}
]
});
webrtc.on('webRTCClientDidReceiveRemoteVideoTrackStream', args => {
const object = args.object;
const remoteVideoTrack = object.get('remoteVideoTrack');
remoteStream = object.get('stream');
const video = frame.topmost().getViewById('remoteVideoView') as WebRTCView;
video.videoTrack = remoteVideoTrack;
});
webrtc.on('webRTCClientStartCallWithSdp', args => {
const sdp = args.object.get('sdp');
const type = args.object.get('type');
if (type === 'answer') {
webrtc.handleAnswerReceived({
sdp: sdp,
type: type
});
} else {
// send data to signaling server
}
});
webrtc.on('webRTCClientDidGenerateIceCandidate', args => {
const iceCandidate = args.object.get('iceCandidate');
// send data to signaling server
});
// Before using getUserMedia verify the app has the permissions and if not try requesting them
if (!WebRTC.hasPermissions()) {
try {
await WebRTC.requestPermissions();
localStream = await webrtc.getUserMedia(Quality.HIGHEST);
} catch (e) {}
}
webrtc.connect();
webrtc.addLocalStream(localStream);
webrtc.makeOffer();
被叫
import { WebRTC } from 'nativescript-webrtc-plugin';
const webrtc = new WebRTC({
enableAudio: true, // default true
enableVideo: false, // default true
iceServers: [
// Optional defaults to google stun servers
{
url: 'stun:stun.l.google.com:19302'
},
{
url: 'serverRequiresAuth',
username: 'username',
password: 'password'
}
]
});
webrtc.on('webRTCClientStartCallWithSdp', args => {
const sdp = args.object.get('sdp');
const type = args.object.get('type') as WebRTCSdpType;
if (type === WebRTCSdpType.ANSWER) {
// send data to signaling server
}
});
webrtc.on('webRTCClientDidGenerateIceCandidate', args => {
const iceCandidate = args.object.get('iceCandidate');
// send data to signaling server
});
// Before using getUserMedia verify the app has the permissions and if not try requesting them
if (!WebRTC.hasPermissions()) {
try {
await WebRTC.requestPermissions();
localStream = await webrtc.getUserMedia(Quality.HIGHEST);
} catch (e) {}
}
webrtc.connect();
webrtc.addLocalStream(localStream);
// sdp received from the signaling server
webrtc.createAnswerForOfferReceived({
type: sdp.type,
sdp: sdp.sdp
});
使用演示
注意 演示可以在没有摄像头的设备上运行,但您需要在此处和此处禁用视频选项(对于核心)或在此处和此处(对于 Angular)禁用视频选项,因为应用程序连接到远程STUN 服务器,因此需要互联网连接。
-
通过运行
npm i && node app
来启动 demo-server -
编辑 socket-server.ts 或 environment.ts 以指向演示服务器运行的计算机的本地 IP 地址
-
在两个设备上输入用户名,然后点击另一台设备的用户名,演示将自动接听电话。 :)
许可证
Apache 许可证版本 2.0,2004 年 1 月