Arsalan Ahmed
Arsalan Ahmed

Reputation: 229

Vonage browser to browser audio call return some error

I am using Laravel, and trying add browser to browser audio calling. I am using Vonage (Tookbox) API for this, but I am getting some error.

here is my code:

 async function audioCall() {
      var publisher;
      var targetElement = 'publisher';
      var pubOptions = {publishAudio:true, publishVideo:false};

      publisher = OT.initPublisher(targetElement, pubOptions, function(error) {
        if (error) {
          alert("The client cannot publish.");
        } else {
          console.log('Publisher initialized.');
        }
      });

      // Setting an audio source to a new MediaStreamTrack
      const stream = await OT.getUserMedia({
        videoSource: null
      });

      const [audioSource] = stream.getAudioTracks();
      publisher.setAudioSource(audioSource).then(() => console.log('Audio source updated'));

      // Cycling through microphone inputs
      let audioInputs;
      let currentIndex = 0;
      OT.getDevices((err, devices) => {
        audioInputs = devices.filter((device) => device.kind === 'audioInput');
        // Find the right starting index for cycleMicrophone
        audioInputs.forEach((device, idx) => {
          if (device.label === publisher.getAudioSource().label) {
            currentIndex = idx;
          }
        });
      });

      const cycleMicrophone = () => {
        currentIndex += 1;
        let deviceId = audioInputs[currentIndex % audioInputs.length].deviceId;
        publisher.setAudioSource(deviceId);
      };
    }

This code return an error on console:

Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules

Upvotes: 0

Views: 164

Answers (1)

Dwane Hemmings
Dwane Hemmings

Reputation: 41

I believe the issue is that you have

device.kind === 'audioInput'

and I'm pretty sure device.kind comes out like 'audioinput' (all lowercase).

examples:

https://developer.mozilla.org/en-US/docs/Web/API/MediaDeviceInfo/kind https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices#examples

That would make audioInputs empty (try to console.log it to verify) and gives you the error because there is no device.

Try:

device.kind.toLowerCase() === 'audioinput'

Hope it works out.

Upvotes: 1

Related Questions