Agora API Addview 函数在 Javascript 中不起作用

我正在尝试使用Agora API

一切都很好,但我的 Javascript 中创建容器以查看我的网络摄像头的 Addview 功能不起作用

我在听完“stream-subscribed”事件后调用它,但它不起作用。

我发现它不监听“stream-added”事件我不知道为什么>

请注意:

我添加了 App_ID 我添加了频道名称 我添加了令牌

感谢帮助

我的网页


<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<title>Basic Live Broadcast</title>

<link rel="stylesheet" href="/css/common.css" />

</head>

<body>

<button id="create"> create</button>

<button id="leave">leave</button>




<script src="/vendor/jquery.min.js"></script>

<script src="/vendor/materialize.min.js"></script>

<script src="/node_modules/agora-rtc-sdk/AgoraRTCSDK.min.js"></script>

<script src="/js/agora_ver4.js"></script>

</body>

</html>


慕桂英4014372
浏览 31回答 1
1回答

慕的地10843

您需要将事件回调移至加入频道成功之外。客户端回调需要在与客户端初始化相同的块内发生。通过等待 joinChannel 成功,为时已晚,引擎在添加侦听器之前就触发了该事件。使用上面的 JS 代码,它应该如下所示:// rtc objectvar rtc = {&nbsp; client: null,&nbsp; joined: false,&nbsp; published: false,&nbsp; localStream: null,&nbsp; remoteStreams: [],&nbsp; params: {}};// Options for joining a channelvar option = {&nbsp; appID: "MY_APP_ID",&nbsp; channel: "MY_Channel_Name",&nbsp; uid: null,&nbsp; token: "Channel_Token"}$('#create').click(function() {&nbsp; // Create a client&nbsp; rtc.client = AgoraRTC.createClient({&nbsp; &nbsp; mode: "live",&nbsp; &nbsp; codec: "h264"&nbsp; });&nbsp; // Initialize the client&nbsp; rtc.client.init(option.appID, function() {&nbsp; &nbsp; console.log("init success");&nbsp; &nbsp; // The value of role can be "host" or "audience".&nbsp; &nbsp; rtc.client.setClientRole("host");&nbsp; &nbsp; // define callbacks before joining the channel&nbsp; &nbsp; rtc.client.on("stream-added", function(evt) {&nbsp; &nbsp; &nbsp; var remoteStream = evt.stream;&nbsp; &nbsp; &nbsp; var id = remoteStream.getId();&nbsp; &nbsp; &nbsp; if (id !== rtc.params.uid) {&nbsp; &nbsp; &nbsp; &nbsp; rtc.client.subscribe(remoteStream, function(err) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("stream subscribe failed", err);&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; console.log('stream-added remote-uid: ', id);&nbsp; &nbsp; });&nbsp; &nbsp; rtc.client.on("stream-subscribed", function(evt) {&nbsp; &nbsp; &nbsp; var remoteStream = evt.stream;&nbsp; &nbsp; &nbsp; var id = remoteStream.getId();&nbsp; &nbsp; &nbsp; // Add a view for the remote stream.&nbsp; &nbsp; &nbsp; addView(id);&nbsp; &nbsp; &nbsp; // Play the remote stream.&nbsp; &nbsp; &nbsp; remoteStream.play("remote_video_" + id);&nbsp; &nbsp; &nbsp; console.log('stream-subscribed remote-uid: ', id);&nbsp; &nbsp; })&nbsp; &nbsp; // Join a channel&nbsp; &nbsp; rtc.client.join(option.token ? option.token : null, option.channel, option.uid ? +option.uid : null, function(uid) {&nbsp; &nbsp; &nbsp; console.log("join channel: " + option.channel + " success, uid: " + uid);&nbsp; &nbsp; &nbsp; rtc.params.uid = uid;&nbsp; &nbsp; &nbsp; // Create a local stream&nbsp; &nbsp; &nbsp; rtc.localStream = AgoraRTC.createStream({&nbsp; &nbsp; &nbsp; &nbsp; streamID: rtc.params.uid,&nbsp; &nbsp; &nbsp; &nbsp; audio: true,&nbsp; &nbsp; &nbsp; &nbsp; video: true,&nbsp; &nbsp; &nbsp; &nbsp; screen: false,&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; // Initialize the local stream&nbsp; &nbsp; &nbsp; rtc.localStream.init(function() {&nbsp; &nbsp; &nbsp; &nbsp; console.log("init local stream success");&nbsp; &nbsp; &nbsp; &nbsp; // Publish the local stream&nbsp; &nbsp; &nbsp; &nbsp; rtc.client.publish(rtc.localStream, function(err) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("publish failed");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.error(err);&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; }, function(err) {&nbsp; &nbsp; &nbsp; &nbsp; console.error("init local stream failed ", err);&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }, function(err) {&nbsp; &nbsp; &nbsp; console.error("client join failed", err)&nbsp; &nbsp; })&nbsp; }, (err) => {&nbsp; &nbsp; console.error(err);&nbsp; });});$('#leave').click(function() {&nbsp; // Leave the channel&nbsp; rtc.client.leave(function() {&nbsp; &nbsp; // Stop playing the local stream&nbsp; &nbsp; rtc.localStream.stop();&nbsp; &nbsp; // Close the local stream&nbsp; &nbsp; rtc.localStream.close();&nbsp; &nbsp; // Stop playing the remote streams and remove the views&nbsp; &nbsp; while (rtc.remoteStreams.length > 0) {&nbsp; &nbsp; &nbsp; var stream = rtc.remoteStreams.shift();&nbsp; &nbsp; &nbsp; var id = stream.getId();&nbsp; &nbsp; &nbsp; stream.stop();&nbsp; &nbsp; &nbsp; removeView(id);&nbsp; &nbsp; }&nbsp; &nbsp; console.log("client leaves channel success");&nbsp; }, function(err) {&nbsp; &nbsp; console.log("channel leave failed");&nbsp; &nbsp; console.error(err);&nbsp; })});function addView(id, show) {&nbsp; if (!$("#" + id)[0]) {&nbsp; &nbsp; $("<div/>", {&nbsp; &nbsp; &nbsp; id: "remote_video_panel_" + id,&nbsp; &nbsp; &nbsp; class: "video-view",&nbsp; &nbsp; }).appendTo("#video");&nbsp; &nbsp; $("<div/>", {&nbsp; &nbsp; &nbsp; id: "remote_video_" + id,&nbsp; &nbsp; &nbsp; class: "video-placeholder",&nbsp; &nbsp; }).appendTo("#remote_video_panel_" + id);&nbsp; &nbsp; $("<div/>", {&nbsp; &nbsp; &nbsp; id: "remote_video_info_" + id,&nbsp; &nbsp; &nbsp; class: "video-profile " + (show ? "" : "hide"),&nbsp; &nbsp; }).appendTo("#remote_video_panel_" + id);&nbsp; &nbsp; $("<div/>", {&nbsp; &nbsp; &nbsp; id: "video_autoplay_" + id,&nbsp; &nbsp; &nbsp; class: "autoplay-fallback hide",&nbsp; &nbsp; }).appendTo("#remote_video_panel_" + id);&nbsp; }}function removeView(id) {&nbsp; if ($("#remote_video_panel_" + id)[0]) {&nbsp; &nbsp; $("#remote_video_panel_" + id).remove();&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5