You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Import WebRTCAdaptor to your project through npmjs.com or as below and add codes PAY ATTENTION THAT YOU NEED TO USE SAME STREAM ID ( stream1Idandstream2Id` variables) IN THE PLAYER.
SAVE THE STREAM IDs OR GIVE RANDOM STATIC VALUES BELOW AND USE THEM IN THE PLAYER.
import{WebRTCAdaptor}from"https://cdn.skypack.dev/@antmedia/webrtc_adaptor@SNAPSHOT";varmediaConstraints={video: false,audio: true};varwebsocketURL="wss://test.antmedia.io/WebRTCAppEE/websocket"//PAY ATTENTION THAT THESE STREAM IDs SHOULD MATCH THE STREAM IDs IN THE PLAYERvarstream1Id="stream"+Math.floor(Math.random()*1000);varstream2Id="stream"+Math.floor(Math.random()*1000);document.getElementById("localAudio1Id").innerHTML=stream1Id;document.getElementById("localAudio2Id").innerHTML=stream2Id;varwebRTCAdaptor1=newWebRTCAdaptor({websocket_url: websocketURL,mediaConstraints: mediaConstraints,localVideoId: "localAudio1",debug: true,callback: function(info,description){if(info=="initialized"){console.log("initialized");document.getElementById("toggleButton").disabled=false}elseif(info=="publish_started"){//stream is being publishedconsole.log("publish started");document.getElementById("status1").innerHTML="Publishing"}elseif(info=="publish_finished"){console.log("publish finished");document.getElementById("status1").innerHTML="Idle"}elseif(info=="closed"){console.log("Connection closed");document.getElementById("status1").innerHTML="Idle"}},callbackError: function(error,message){//some of the possible errors, NotFoundError, SecurityError,PermissionDeniedErroralert("error: "+error+" message:"+message);}});//currently Ant Media Server accepts one streams from the same webrtc session so we create the second webrtc adaptorvarwebRTCAdaptor2=newWebRTCAdaptor({websocket_url: websocketURL,mediaConstraints: mediaConstraints,localVideoId: "localAudio2",debug: true,callback: function(info,description){if(info=="initialized"){console.log("initialized");}elseif(info=="publish_started"){//stream is being publishedconsole.log("publish started");document.getElementById("status2").innerHTML="Publishing"}elseif(info=="publish_finished"){console.log("publish finished");document.getElementById("status2").innerHTML="Idle"}elseif(info=="closed"){console.log("Connection closed");document.getElementById("status2").innerHTML="Idle"}},callbackError: function(error,message){//some of the possible errors, NotFoundError, SecurityError,PermissionDeniedErroralert("error: "+error+" message:"+message);}});//just a dummy checkvarisPublishing=false;document.getElementById("toggleButton").addEventListener("click",functiontogglePublishing(){if(!isPublishing){webRTCAdaptor1.publish(stream1Id);webRTCAdaptor2.publish(stream2Id);document.getElementById("toggleButton").innerHTML="Stop";}else{webRTCAdaptor1.stop(stream1Id);webRTCAdaptor2.stop(stream2Id);document.getElementById("toggleButton").innerHTML="Start";}isPublishing=!isPublishing;});
Import WebRTCAdaptor to your project through npmjs.com or as below and add codes
PAY ATTENTION THAT YOU NEED TO USE SAME STREAM ID( stream1Id and stream2Id variables) THAT YOU USE IN THE PUBLISHER
import{WebRTCAdaptor}from"https://cdn.skypack.dev/@antmedia/webrtc_adaptor@SNAPSHOT";//PAY ATTENTION THAT YOU NEED TO USE SAME STREAM ID THAT YOU USE IN THE PUBLISHERvarstream1Id="stream585";varstream2Id="stream531";varwebsocketURL="wss://test.antmedia.io/WebRTCAppEE/websocket"document.getElementById("streamId1").innerHTML=stream1Id;document.getElementById("streamId2").innerHTML=stream2Id;varwebRTCAdaptor=newWebRTCAdaptor({websocket_url: websocketURL,isPlayMode: true,callback: function(info,description){if(info=="initialized"){console.log("initialized");document.getElementById("toggleButton").disabled=false}elseif(info=="play_started"){//stream is being publishedconsole.log("play started for "+description.streamId);if(description.streamId==stream1Id){document.getElementById("status1").innerHTML="Playing";}else{document.getElementById("status2").innerHTML="Playing";}}elseif(info=="play_finished"){console.log("play finished");if(info.streamId==stream1Id){document.getElementById("status1").innerHTML="Idle";}else{document.getElementById("status2").innerHTML="Idle";}}elseif(info=="closed"){console.log("Connection closed");document.getElementById("status1").innerHTML="Idle";document.getElementById("status2").innerHTML="Idle"}elseif(info=="newTrackAvailable"){console.log("newTrackAvailable "+JSON.stringify(description))if(description.streamId==stream1Id){document.getElementById("audioPlayer1").srcObject=newMediaStream();document.getElementById("audioPlayer1").srcObject.addTrack(description.track);}elseif(description.streamId==stream2Id){document.getElementById("audioPlayer2").srcObject=newMediaStream();document.getElementById("audioPlayer2").srcObject.addTrack(description.track);}}},callbackError: function(error,message){//some of the possible errors, NotFoundError, SecurityError,PermissionDeniedErroralert("error: "+error+" message:"+message);}});//just a dummy checkvarisPlaying=false;document.getElementById("toggleButton").addEventListener("click",function(){if(!isPlaying){webRTCAdaptor.play(stream1Id);webRTCAdaptor.play(stream2Id);document.getElementById("toggleButton").innerHTML="Stop";}else{webRTCAdaptor.stop(stream1Id);webRTCAdaptor.stop(stream2Id);document.getElementById("toggleButton").innerHTML="Start";}isPlaying=!isPlaying;});
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi Guys,
Here are simple samples to publish two audio streams in one page and play the same audio streams in another page.
Publish Two Audio Streams
Live Sample is available on https://codepen.io/mekya/pen/dyQmPee
PAY ATTENTION THAT YOU NEED TO USE SAME STREAM ID ( stream1Id
and
stream2Id` variables) IN THE PLAYER.SAVE THE STREAM IDs OR GIVE RANDOM STATIC VALUES BELOW AND USE THEM IN THE PLAYER.
Play Two Audio Stream
Live sample is available on https://codepen.io/mekya/pen/JjeLYpY
PAY ATTENTION THAT YOU NEED TO USE SAME STREAM ID(
stream1Id
andstream2Id
variables) THAT YOU USE IN THE PUBLISHERBeta Was this translation helpful? Give feedback.
All reactions