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/rawfor custom playback.
Quick start
Core concepts
CometChatSoundManager: Manages playback of UI Kit sounds.Sound: Enum of supported sound events such asincomingCallandoutgoingMessage.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
CometChatSoundManagerwith a validContext. - Code:
- Kotlin
- Java
SoundManagerHelper.kt
- 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:
- Kotlin
- Java
SoundManagerUsage.kt
- 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 change | Where | Property/API | Example |
|---|---|---|---|
| Create sound manager | app/src/main/java/<your_package>/SoundManagerHelper.kt | CometChatSoundManager(Context) | val soundManager = CometChatSoundManager(context) |
| Play default sounds | app/src/main/java/<your_package>/SoundManagerUsage.kt | play(Sound) | soundManager.play(Sound.incomingCall) |
| Play custom sounds | app/src/main/java/<your_package>/SoundManagerUsage.kt | play(Sound, int) | soundManager.play(Sound.incomingCall, R.raw.incoming_call) |
| Pause playback | app/src/main/java/<your_package>/SoundManagerUsage.kt | pause() | soundManager.pause() |
Common pitfalls and fixes
- No sound plays: Ensure
CometChatSoundManageris created with a validContext. - Custom sounds not found: Place audio files in
app/src/main/res/rawand reference them asR.raw.<file>. - Sound plays once only: Reuse the same
CometChatSoundManagerinstance 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.