I have implemented audio call functionality using Agora Javascript SDK 4. in one of my sites and facing an issue that local user voice is not streaming to remote users and when I disable and enable mic using Browser console it works, I don't know why this happening Here is the code I am using 4.6.3 and below CDN I have used.
CDN :
<script src="https://download.agora.io/sdk/release/AgoraRTC_N-4.6.3.js"></script>
var rtc = {
client: null,
localAudioTrack: null,
};
let searchParams = new URLSearchParams(window.location.search)
let channel = searchParams.get('cid')
var options = {
appId: "<appId>",
channel: channel,
token: null,
};
async function startBasicCall() {
rtc.client = AgoraRTC.createClient({ mode: "rtc", codec: "vp8" });
const uid = await rtc.client.join(options.appId, options.channel, options.token, null);
rtc.localAudioTrack = await AgoraRTC.createMicrophoneAudioTrack();
rtc.localAudioTrack.setEnabled(true);
await rtc.client.publish([rtc.localAudioTrack]);
rtc.client.on("user-published", async (user, mediaType) => {
await rtc.client.subscribe(user, mediaType);
if (mediaType === "audio") {
const remoteAudioTrack = user.audioTrack;
remoteAudioTrack.play();
}
});
rtc.client.on("user-unpublished", user => {
const playerContainer = document.getElementById(user.uid);
playerContainer.remove();
});
}
async function leaveCall() {
rtc.localAudioTrack.close();
await rtc.client.leave();
}
if(channel != "" && channel != null){
startBasicCall();
}
I have a dynamic channel Name that will be collected from the URL. I have 2 users User A: calls User B User B : B is able to hear voice of A but B users voice not streaming to A so A is not able to hear anything
But when i run below code on user A's Browser console then both user can hear each others voice rtc.localAudioTrack.setEnabled(false); //this is for muting rtc.localAudioTrack.setEnabled(true); // this is for unmute
FYI : I have used same code for both end.
I think the problem is because you're attaching event handlers after the channel join. You should do it before you invoke the join method.