Skip to main content
Use CometChatSoundManager to play UI Kit audio cues for calls and messages.

When to use this

  • You want audible feedback for incoming or outgoing calls and messages.
  • You need custom sounds for specific chat events.
  • You want to pause or stop sound playback programmatically.

Prerequisites

  • CometChat UI Kit for Android is installed and initialized.
  • You have access to an Android Context.
  • Optional: You have sound files in app/src/main/res/raw for custom playback.

Quick start

1

Create a sound manager

Instantiate CometChatSoundManager using a valid Context.
2

Play default sounds

Call play(Sound) for built-in UI Kit sounds.
3

Play custom sounds

Call play(Sound, int) with a file from res/raw.
4

Pause playback

Call pause() when you need to stop sound playback.
5

Build and verify

Run the app and confirm the expected sound plays for each event.

Core concepts

  • CometChatSoundManager: Manages playback of UI Kit sounds.
  • Sound: Enum of supported sound events such as incomingCall and outgoingMessage.
  • R.raw: Android raw resource IDs used for custom sounds.

Available methods

  • play(Sound sound): Plays the default UI Kit sound for the given event.
  • play(Sound sound, int res): Plays a custom raw resource for the given event.
  • pause(): Pauses any sound currently playing.

Implementation

Initialize CometChatSoundManager

What you’re changing: Creating a sound manager instance.
  • Where to change it: Any class where you have access to a Context.
  • Applies to: All UI Kit sound playback.
  • Default behavior: Sounds are not managed until you create the instance.
  • Override: Instantiate CometChatSoundManager with a valid Context.
  • Code:
SoundManagerHelper.kt
import android.content.Context
import com.cometchat.chatuikit.shared.resources.soundmanager.CometChatSoundManager

fun createSoundManager(context: Context): CometChatSoundManager {
    return CometChatSoundManager(context)
}
  • What this does: Creates a sound manager you can reuse across your UI Kit screens.
  • Verify: Call a play method and confirm audio plays.

Play default and custom sounds

What you’re changing: Triggering sound playback for chat events.
  • Where to change it: The class that handles your chat or call UI logic.
  • Applies to: Message and call events you choose to handle.
  • Default behavior: UI Kit plays its built-in sounds when triggered.
  • Override: Use play(Sound, int) to play a custom raw resource.
  • Code:
SoundManagerUsage.kt
import android.content.Context
import com.cometchat.chatuikit.shared.resources.soundmanager.CometChatSoundManager
import com.cometchat.chatuikit.shared.resources.soundmanager.Sound

fun playSounds(context: Context) {
    val soundManager = CometChatSoundManager(context)

    // Default sounds
    soundManager.play(Sound.incomingCall)
    soundManager.play(Sound.outgoingCall)
    soundManager.play(Sound.incomingMessage)
    soundManager.play(Sound.outgoingMessage)

    // Custom sounds
    soundManager.play(Sound.incomingCall, R.raw.incoming_call)
    soundManager.play(Sound.outgoingCall, R.raw.outgoing_call)
    soundManager.play(Sound.incomingMessage, R.raw.incoming_message)
    soundManager.play(Sound.outgoingMessage, R.raw.outgoing_message)

    // Pause playback
    soundManager.pause()
}
  • What this does: Plays default and custom sounds, then pauses playback.
  • Verify: You hear the expected sound for each call and message event.

Customization matrix

What you want to changeWhereProperty/APIExample
Create sound managerapp/src/main/java/<your_package>/SoundManagerHelper.ktCometChatSoundManager(Context)val soundManager = CometChatSoundManager(context)
Play default soundsapp/src/main/java/<your_package>/SoundManagerUsage.ktplay(Sound)soundManager.play(Sound.incomingCall)
Play custom soundsapp/src/main/java/<your_package>/SoundManagerUsage.ktplay(Sound, int)soundManager.play(Sound.incomingCall, R.raw.incoming_call)
Pause playbackapp/src/main/java/<your_package>/SoundManagerUsage.ktpause()soundManager.pause()

Common pitfalls and fixes

  • No sound plays: Ensure CometChatSoundManager is created with a valid Context.
  • Custom sounds not found: Place audio files in app/src/main/res/raw and reference them as R.raw.<file>.
  • Sound plays once only: Reuse the same CometChatSoundManager instance instead of creating one per event.
  • Silent device: Check device volume and ringer mode.
  • No sound on specific devices: Verify the audio file format is supported by Android.

FAQ

Q: Do I need to initialize SoundManager before each play call?
A: No. Create one instance and reuse it while the screen is active.
Q: Can I use my own audio files?
A: Yes. Place them in res/raw and call play(Sound, int) with your R.raw resource ID.
Q: How do I stop a sound that is playing?
A: Call pause() on the sound manager instance.