ConnectyCube / connectycube-web-samples

Chat and Video Chat code samples for Web, based on ConnectyCube platform
https://connectycube.com
Apache License 2.0
18 stars 29 forks source link

I am unable to switch audio tracks in web development ,But it working for mobiles. #282

Open Govind9010 opened 5 months ago

Govind9010 commented 5 months ago

` changeAudio(option?: any) {

const constraints = { audio: option.deviceId };
console.log('this.session :::', this.session);
this.session
  .switchMediaTracks(constraints)
  .then((stream: MediaStream) => {
    console.log('stream :::', stream);
    const audioTracks = stream.getAudioTracks();
    if (audioTracks.length > 0) {
      // Remove existing audio tracks from local stream
      this.session.localStream.getAudioTracks().forEach((track: { stop: () => void; }) => {
        track.stop();
        this.session.localStream.removeTrack(track);
      });
      // Add new audio tracks to local stream
      audioTracks.forEach(track => {
        console.log('trackkkkkk', track);
        this.session.localStream.addTrack(track);
      });
      // Store the new audio tracks for future reference
      this.strm = audioTracks;
    }
  })
  .catch((error: any) => { console.log(error) });

} ` i am using above code .in option parameter we got device as we are selected device. But i am unable switch the audio track in web. Please respond on my issue

DaveLomber commented 5 months ago

@Govind9010 could you describe your use case in details. Are you looking to switch between different mics during a call?

Govind9010 commented 5 months ago

Screenshot 2024-04-17 142829 I am using below code to switch audio speakers during call .

` changeAudio(element: any, option: string) {

  const constraints = { audio: option };

  this.session
    .switchMediaTracks(constraints)
    .then((stream: MediaStream) => {
      const [audioTracks] = stream.getAudioTracks();
      console.log('audioTracksssss', [audioTracks]);
      if ([audioTracks].length > 0) {
        // Remove existing audio tracks from local stream
        this.session.localStream.getAudioTracks().forEach((track: { stop: () => void; }) => {
          track.stop();
          this.session.localStream.removeTrack(track);
        });
        // Add new audio tracks to local stream
        [audioTracks].forEach(track => {
          console.log('trackkkkkk', track);
          this.session.localStream.addTrack(track);
        });
        // Store the new audio tracks for future reference
        this.strm = [audioTracks];
      }
    })
    .catch((error: any) => { console.log(error) });
}

`

  1. Switching speaker connection during call is working in Mobiles (Bluetooth connection to mobile speaker connection ). Same use case not working for PC or WEB . Thanks.
DDooni commented 5 months ago

Greetings from ConnectyCube team!

@Govind9010 try this approach:

class CallService {
  session = {}; // ... your session

  async switchAudio(deviceId) {
    const constraints = {
      video: true,
      audio: {
        deviceId,
      },
    };
    let stream = await this.getUserStream(constraints);
    stream = await this.updateStream(stream, constraints);

    // do whatever you want with your stream object
  }

  async getUserStream(constraints) {
    return navigator.mediaDevices.getUserMedia(constraints);
  }

  async updateStream(stream, constraints) {
    return this.session.updateStream(
      stream,
      constraints,
      true, // should update stream or not
      null,
      null,
      false,
      false, // was audio previously muted
      false,
      false
    );
  }

  // ... the rest of your code
}

Hope this works for you.

Happy to help!