setAudioInterruption
Set Audio Interruption State
setAudioInterruption is the core method in the SUDRTGameAudioSession interface for handling external interference (such as incoming calls, system alarms, or voice assistant wake-ups). It allows the App to manually suspend or resume the game's audio rendering process, ensuring reasonable allocation of device audio resources in non-game scenarios.
Method Declaration
java
/**
* Set the audio interruption state of the current game instance.
* @param var1 Pass true to trigger an interruption (suspend audio); pass false to clear the interruption (resume audio).
*/
void setAudioInterruption(boolean var1);Runtime Behavior
- Resource Suspension: When
setAudioInterruption(true)is called, the SDK pauses processing of the audio track. Unlikemute, an interruption usually means the audio engine enters a "standby" state, reducing unnecessary performance overhead. - Focus Coordination: On the Android system, when another app (such as the dialer) requests
AUDIOFOCUS_GAIN, the App should proactively call this method. - State Recovery: When the external interference ends (e.g., the call is hung up), calling
setAudioInterruption(false)smoothly resumes the game audio from the interruption point, without re-initializing the audio session.
Code Example
Handle audio interruptions in an Android Activity's lifecycle or a broadcast receiver:
java
// Get the audio session control object
SUDRTGameAudioSession audioSession = mGameHandle.getAudioSession();
// Simulate listening for system phone state changes
public void onPhoneStateChanged(int state) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
// 1. Phone ringing, immediately interrupt game audio to avoid disturbing the call
audioSession.setAudioInterruption(true);
} else if (state == TelephonyManager.CALL_STATE_IDLE) {
// 2. Call ended, resume game audio
audioSession.setAudioInterruption(false);
}
}
// It is also recommended to sync this in the Activity lifecycle
@Override
protected void onPause() {
super.onPause();
// It is generally recommended to also set an interruption when moving to the background
audioSession.setAudioInterruption(true);
}Notes
- Combine with Lifecycle: It is strongly recommended to use this method in pairs in
onPauseandonResume, to comply with Android green app guidelines and prevent the App from continuing to occupy the audio channel after moving to the background. - VOIP Conflict: If your App has native voice call functionality, you must set
setAudioInterruption(true)when starting a call, otherwise the game sound may be fed back through the microphone and produce an echo. - Difference from Mute:
muteonly sets the volume to zero, whilesetAudioInterruptionaffects the active state of the underlying audio stream.
