mute
Set Mute State
mute: is the audio switch method of the SUDRTGameAudioSession protocol. It is used to immediately enable or disable all audio output of the current game instance at the software level. This method provides a global "one-tap mute" feature, commonly used when the App switches to the background, the user enables Do Not Disturb mode, or specific business logic (such as answering a phone call) is triggered.
Method Declaration
objc
/**
* Sets game audio to muted or resumes playback.
* @param mute YES indicates immediate mute; NO indicates resuming playback at the previous volume.
*/
- (void)mute:(BOOL)mute;Runtime Behavior
- Global interception: When the parameter is
YES, the SDK cuts off the output of all game audio tracks. This does not affect the game logic's execution; it only blocks the audio stream from reaching the system mixer. - Volume retention: Calling
mute:YESdoes not modify the value ofVolumeFactor. Whenmute:NOis called again to restore sound, the game volume automatically returns to the scaling ratio before muting. - State isolation: This mute switch is effective only for the current SDK instance and does not affect the sound playback of other Apps in the iOS system (AVAudioSession).
- Non-persistence: This state typically becomes invalid when the game instance is destroyed. If you want the player to remain muted the next time they enter the game, the App side needs to record this configuration itself.
Code Example
Automatically mute the game when the App detects an incoming system call:
objc
- (void)handleIncomingCall {
// 1. When the business logic is triggered, immediately mute the game
if (![self.audioSession isMute]) {
[self.audioSession mute:YES];
NSLog(@"External interference detected, game auto-muted");
}
}
- (void)onMuteToggleChanged:(UISwitch *)sender {
// 2. Set mute based on the UI switch state
[self.audioSession mute:sender.isOn];
}Notes
- Difference from hardware mute: This method controls "software mute". If the user enables muting via the physical mute switch on the side of the iPhone, the game may still be silent even if
mute:NOis called (depending on theSUDRT_KEY_AUDIO_OBEY_MUTE_SWITCHconfiguration). - Performance recommendation: Calling
mute:frequently in a short period may cause the underlying audio pipeline to be repeatedly initialized. It is recommended to add debouncing at the UI layer. - State query: Before executing logic, you can first determine the current state via the
isMutemethod to avoid redundantly setting the same state.
