声网de帮助文档网址
var options = {
streamID: uid,
audio: true,
video: true,
screen: false,
extensionId: "minllpmhdgpndnkomcoccfekfegnlikg"
}
var localStream = AgoraRTC.createStream(options);
设置本地流 Profile,详见 设置视频属性 (setVideoProfile) 。
localStream.setVideoProfile("480p_4");
初始化本地流,详见 初始化音视频对象(init) 。
localStream.init(function(){
console.log("Local stream initialized");
});
发布本地流,详见 上传本地音视频流 (publish) 。
client.publish(localStream, function(err){
console.log("Publish stream failed", err);
});
监听流事件和订阅远端流,详见 远程音视频流已添加回调事件 (stream-added) 和 订阅远程音视频流 (subscribe) 。
client.on('stream-added', function(evt) {
var stream = evt.stream;
console.log("New stream added: " + stream.getId());
console.log("Timestamp: " + Date.now());
console.log("Subscribe ", stream);
client.subscribe(stream, function(err) {
console.log("Subscribe stream failed", err);
});
});
client.on('peer-leave', function(evt) {
console.log("Peer has left: " + evt.uid);
console.log("Timestamp: " + Date.now());
console.log(evt);
});
client.on('stream-subscribed', function(evt) {
var stream = evt.stream;
console.log("Got stream-subscribed event");
console.log("Timestamp: " + Date.now());
console.log("Subscribe remote stream successfully: " + stream.getId());
console.log(evt);
});
client.on("stream-removed", function(evt) {
var stream = evt.stream;
console.log("Stream removed: " + evt.stream.getId());
console.log("Timestamp: " + Date.now());
console.log(evt);
});
播放本地/远端流,详见 播放音视频流 (play) 。
localStream.play("agora-remote");
安装好示例代码后,输入你想要进入的房间名,请确保在音视频通话之前开通你的摄像头和麦克风权限。 场景 2 :网页端屏幕共享如果想在网页端音视频通话过程中,启用屏幕共享功能,在使用示例代码前,你还需要进行如下操作: 1. 获取屏幕共享插件联系 sales@agora.io 或技术支持获取 Chrome 屏幕共享插件,并将获取到的插件包解压。完整的插件文件夹结构如下图所示: 2. 安装插件并获取插件 ID在 Chrome 网页端安装获取到的 Chrome 插件。首先打开你的 Chrome 浏览器,点击屏幕右上方的扩展按钮,选择 更多工具 > 扩展程序。  然后点击 加载已解压的扩展程序 ,选择你刚刚获取并解压的 Chrome 插件文件夹,然后点击 选择 。  完成插件安装后,你可以直接在 Chrome 浏览器界面查看你的插件 ID。 
3. 修改域名点击打开插件文件夹中的 json 文件夹,然后将 match 行的域名替换为你的网页域名。 4. 部署 Sample code1)创建 Client 对象,详见 创建音视频对象 (createClient) 。
var client = AgoraRTC.createClient({mode:'interop'});
2)初始化 Client 对象,详见 初始化客户端对象 (init) 。
client.init(appid, function(){
console.log("AgoraRTC client initialized");
});
3)加入频道,详见 加入 AgoraRTC 频道 (join) 。
client.join(null, "webtest", undefined, function(uid){
console.log("User " + uid + " join channel successfully");
console.log("Timestamp: " + Date.now());
});
4)创建本地流,详见 创建音视频流对象 (createStream) 。注意将代码中的 extensionId 替换成刚刚获取的插件 ID,详见 2. 安装插件并获取插件 ID 。 //创建本地流, 设置audio/video为false, screen为true, extensionID请提供属于你自己的chrome分享屏幕插件ID
//共享的流与一般的流几乎没有区别, 拥有一样的事件与生命周期, 但不能用来控制音频或视频
/*
RTC.createStream
@param: options
@param: options.streamId id of stream
@param: options.audio if audio is captured locally
@param: options.video if video is captured locally
@param: options.screen if this stream is for screen share
@param: options. extensionId the extension id of your chrome extension if share screen is enabled
return: created local stream
*/
var options = {
streamID: uid,
audio: false,
video: false,
screen: true,
//chrome extension id
extensionId: "minllpmhdgpndnkomcoccfekfegnlikg"
}
var localStream = AgoraRTC.createStream(options);
5)设置本地流 Profile,详见 设置视频属性 (setVideoProfile) 。
localStream.setVideoProfile("480p_4");
6)初始化本地流,详见 初始化音视频对象(init) 。
localStream.init(function(){
console.log("Local stream initialized");
});
7)发布本地流,详见 上传本地音视频流 (publish) 。
client.publish(localStream, function(err){
console.log("Publish stream failed", err);
});
8)监听流事件和订阅远端流,详见 远程音视频流已添加回调事件 (stream-added) 和 订阅远程音视频流 (subscribe) 。
client.on('stream-added', function(evt) {
var stream = evt.stream;
console.log("New stream added: " + stream.getId());
console.log("Timestamp: " + Date.now());
console.log("Subscribe ", stream);
client.subscribe(stream, function(err) {
console.log("Subscribe stream failed", err);
});
});
client.on('peer-leave', function(evt) {
console.log("Peer has left: " + evt.uid);
console.log("Timestamp: " + Date.now());
console.log(evt);
});
client.on('stream-subscribed', function(evt) {
var stream = evt.stream;
console.log("Got stream-subscribed event");
console.log("Timestamp: " + Date.now());
console.log("Subscribe remote stream successfully: " + stream.getId());
console.log(evt);
});
client.on("stream-removed", function(evt) {
var stream = evt.stream;
console.log("Stream removed: " + evt.stream.getId());
console.log("Timestamp: " + Date.now());
console.log(evt);
});
9)播放本地/远端流,详见 播放音视频流 (play) 。
localStream.play("agora-remote");
安装好示例代码后,你就可以开始屏幕共享了。 如果想退出屏幕共享,可以参考如下代码的逻辑: function deinitShare(client, stream) {
if (stream) {
client.unpublish(stream, function (err) {
console.log("Unpublish failed with error: ", err);
});
stream.close();
client.leave();
}
}
|