Show Audio Levels

The concepts explained here are:

  1. Getting Audio Levels for all speaking peers.
  2. Creating a UI that shows the loudest speaking users.
  3. Dominant Speaker - the loudest speaker.

Audio Level

The audio level for every speaking peer can be retrieved by registering an instance of HMSAudioListener in hmsSDK.addAudioObserver like so.

hmsSDK.addAudioObserver(object : HMSAudioListener { override fun onAudioLevelUpdate(speakers: Array<HMSSpeaker>) { Log.v("TAG", "Active Speakers are: ${speakers.map { s -> "${s.peer?.name} ${s.level}" }}}" ) } })

Here are the properties on the HMSSpeaker class:

  • trackId : String. The track id of the audio track for this speaker.
  • level : Int. The level of the audio. It may vary from 0-100. A higher value indicates a higher speaking volume.
  • hmsTrack : HMSTrack?. The audio track corresponding to this speaker. It may be null when the speaker who was speaking loudly, leaves.
  • peer : HMSPeer?. The peer who was speaking. This may be null if the peer has left before the update information has reached the other person.

Active Speaker Views

To maintain an active speaker view, such as the default view in the open source advanced sample app, you need to keep track of who the active speakers are over time. We'll be using the input from HMSAudioListener as mentioned above and then building something that attempts to show all the active speakers while minimizing re-ordering the list.

This is achived in the sample app by means of the ActiveSpeakerCache, ActiveSpeakerHandler and then piping the input from the HMSAudioListener into the handler as demonstrated here.

Dominant Speaker

The dominant speaker is the speaker who's the loudest at any given moment. There's a callback for this in the onPeerUpdate callback for HMSUpdateListener.

Here's the method signature for onPeerUpdate: override fun onPeerUpdate(type: HMSPeerUpdate, hmsPeer: HMSPeer)

When the dominant speaker has changed, there will be updates of the type:

  • HMSPeerUpdate.BECAME_DOMINANT_SPEAKER When a new person becomes the dominant speaker.

  • HMSPeerUpdate.NO_DOMINANT_SPEAKER When the current dominant speaker stops talking and there's no one to replace them.

The hmsPeer parameter contains the person these updates apply to.