I checked whats app and the send/receive message sounds play only on NOTIFICATION stream on android when you're inside a chat.
We have four stream channels in android:
- Ringtone
- Media
- Notification
- System
I want to play sound on Notification stream.
Sample Code that doesn't seem to work:
try {
with(mediaPlayer) {
reset()
setDataSource(
requireContext(),
Uri.parse("android.resource://${requireContext().packageName}/" + R.raw.sound_all_outgoingmessage)
)
setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
setAudioAttributes(
AudioAttributes
.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build())
prepare()
setOnCompletionListener { play = true }
if (!isPlaying && play) {
play = false
start()
}
}
} catch (e: Exception) {
play = true
e.printStackTrace()
}