I'm making an app that connects to OpenAI's Realtime API using WebRTC.
My Mute Microphone mutes the microphone, correctly. But what if I want to also Mute the output from the Realtime API?
The whole code is here: https://github.com/Sophon/openAiTTS
But the relevant code:
internal class WebRTCClient (
onIncomingEvent: (OpenAiEvent) -> Unit,
context: Context,
) {
private val peerConnectionFactory: PeerConnectionFactory
private val peerConnection: PeerConnection
private val negotiateJob = AtomicReference<Job?>(null)
private val audioSource: AudioSource
private val localAudioTrack: AudioTrack
private val dataChannel: DataChannel
init {
peerConnectionFactory = createPeerConnectionFactory(context)
peerConnection = createPeerConnection()
createAudioSourceAndTrack().let {
audioSource = it.first
localAudioTrack = it.second
}
val sender = peerConnection.addTrack(localAudioTrack)
}
private fun createAudioSourceAndTrack(): Pair<AudioSource, AudioTrack> {
Napier.d(tag = TAG) { "Audio track: creating and adding" }
val audioSource: AudioSource = peerConnectionFactory.createAudioSource(MediaConstraints())
val localAudioTrack = peerConnectionFactory.createAudioTrack("mic", audioSource)
localAudioTrack.setEnabled(true)
return Pair(audioSource, localAudioTrack)
}
}
What I've tried:
fun setAudioTrackEnabled(enabled: Boolean) {
localAudioTrack.setEnabled(enabled)
}
This doesn't do anything, the output from the AI continues to play.
How do I mute the output from remote?
In WebRTC, output audio comes through remote media stram.Muting those required you to identify and disable the remote audio track once they're received.
You need to keep a rerfernce to the remote audio tracks and mute them.
try below code :
remoteAudioTrack.setEnable(false)
try to replace
private val localAudioTrack: AudioTrack
with
private var remoteAudioTrack : AudioTrack?=null
Here is peerConnection code (EDIT Code):
private fun createPeerConnection():PeerConnection{
val rtcConfig = PeerConnection.RTCConfiguration(emptyist())
return peerConnectionFactory.createPeerConnection(rtcConfig, object : PeerConnection.Observer {
override fun onTrack(transceiver: RtpTransceiver) {
val mediaStreamTrack = transceiver.receiver.track()
if (mediaStreamTrack is AudioTrack) {
remoteAudioTrack = mediaStreamTrack
remoteAudioTrack?.setEnabled(true) // set to false to mute output
Log.d("WebRTC", "Remote audio track received and attached")
}
}
// You can optionally still handle `onAddStream()` if your signaling setup uses that.
})!!
}
fun muteRemoteAudio(mute:Boolean){
remoteAudioTrack?.setEnable(!mute)
}