Audio tracking with agora

35 views Asked by At

I would like to find out how long each person speaks during an online meeting using Agora Web. I looked around and tried writing some code to accomplish this but it didn't work as expected.

Is there a better way to calculate the duration each person talks in a meeting using Agora Web?

let handleUserJoined = async (user, mediaType) => {
    console.log('User joined')
    remoteTracks[user.uid] = user

    await client.subscribe(user, mediaType)

    if (mediaType === 'video'){
        let videoPlayer = `<div class = "video-container" id = "video-wrapper-${user.uid}">
                            <p class = "user-id" >${user.uid}</p>
                            <div class = "video-player player"  id="stream-${user.uid}"></div> 
                        </div>`
    document.getElementById('user-streams').insertAdjacentHTML('beforeend', videoPlayer)
    user.videoTrack.play(`stream-${user.uid}`)
    }

    if(mediaType === 'audio'){
        user.audioTrack.play();
    }
}
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const analyser = audioContext.createAnalyser();
analyser.fftSize = 2048;
const bufferLength = analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);

// Set the source of the analyser node to the remote audio stream
client.on('stream-subscribed', (evt) => {
 const remoteStream = evt.stream;
 const remoteAudioTrack = remoteStream.getAudioTracks()[0];
 const source = audioContext.createMediaStreamSource(new MediaStream([remoteAudioTrack]));
 source.connect(analyser);

setInterval(() => {
    analyser.getByteTimeDomainData(dataArray);
 
    // Calculate the audio duration by counting the number of samples in the audio waveform data
    const audioDuration = dataArray.length / audioContext.sampleRate;
    console.log('Audio duration: ', audioDuration);
  }, 1000);
 });
0

There are 0 answers