Skip to main content
Version: 5.5

Audio hooking

PlanetKit provides the audio hooking function, which allows you to get the audio data before Planet Cloud transmits audio to a peer. This function can be used to modify the local user's voice or use the audio data for speech recognition.

Supported call typeMinimum SDK version
1-to-1 call5.5

Overview

The PlanetKitCall class provides methods to use the audio hooking function.

Enable audio hooking

To enable audio hooking, call enableHookMyAudio() with the implementation of PlanetKitHookMyAudioListener.

When audio hooking is enabled, you can receive a PlanetKitHookedAudio object through the onHooked event.

Modify audio data (Optional)

To modify the hooked audio data, take the following steps:

  1. Call getRawData() of PlanetKitHookedAudio to get the audio data.
  2. Modify the audio data as necessary.
  3. Call setRawData() of PlanetKitHookedAudio to set the modified audio data.
Note

In the PlanetKitHookedAudio class, only the raw audio data (data) can be modified, and metadata such as channel, sampleCount, sampleRate, sampleType, and seq cannot be changed. When you modify data, make sure that the format represented by the metadata is maintained.

Put back the audio data

To put the hooked audio back for transmission, call putHookedMyAudioBack() with a PlanetKitHookedAudio object.

Warning

After enabling audio hooking, you must put back the audio regardless of whether the audio data has been modified. Otherwise, audio is not transmitted to the peer.

Disable audio hooking

To disable audio hooking, call disableHookMyAudio().

Sample code

The following sample code shows how to use the audio hooking function for voice modification.

// Get instance of PlanetKitCall
private var call: PlanetKitCall?

// Single-threaded executor to handle audio processing asynchronously
private val executor = Executors.newSingleThreadExecutor()

// PlanetKitHookMyAudioListener object
private val listener = PlanetKitHookMyAudioListener { audioData ->
processingAudio(audioData) // Pass intercepted audio data to be processed
}

// Method to process and modify the audio data
private fun processingAudio(audioData: PlanetKitHookedAudio) {
// Submit the audio processing task to the executor
executor.submit {
// Get the raw audio data
val processedAudio: ByteArray = audioData.getRawData()

// Modify the audio data as needed
...

// Set the modified audio back
audioData.setRawData(processedAudio)

// Send the processed audio data back to the server
call?.putHookedMyAudioBack(audioData)
}
}

// Method to enable audio hook
fun enableAudioHook() {
call?.enableHookMyAudio(listener, PlanetKitRequestCallback {
if (it.isSuccessful) {
Log.d(TAG, "Audio hook enabled successfully")
} else {
Log.e(TAG, "Failed to enable audio hook")
}
})
}

// Method to disable the audio hook
fun disableAudioHook() {
call?.disableHookMyAudio(PlanetKitRequestCallback {
if (it.isSuccessful) {
Log.d(TAG, "Audio hook disabled successfully")
} else {
Log.e(TAG, "Failed to disable audio hook")
}
})
}

APIs related to audio hooking are as follows.

Classes/interfaces

Methods

Events