getGameAudioSession
Get the Audio Control Session
getGameAudioSession returns the audio management object SUDRTGameAudioSession created by the Runtime Core. This object is the sole entry point for the host app to control the mini-game's audio behavior, allowing developers to fine-tune the game's global volume, mute state, and audio focus at the native layer.
Method Declaration
java
/**
* Returns the SUDRTGameAudioSession created by the Runtime Core for controlling mini-game audio.
* * @return SUDRTGameAudioSession the audio session instance
*/
SUDRTGameAudioSession getGameAudioSession();Core Features
Through the returned SUDRTGameAudioSession object, developers can typically implement the following business logic:
- Global volume adjustment: Adjust the mixed volume output by the mini-game internally without changing the system media volume.
- One-tap mute: Quickly enable or disable all sound output of the mini-game (commonly used when the app switches to the background or enters silent mode).
- Audio focus coordination: When the host app needs to play a voice message or background music, use this Session to temporarily duck or pause the mini-game's sound.
Runtime Behavior
- Singleton binding: Each
SUDRTGameHandleinstance corresponds to a uniqueAudioSession. - Lifecycle synchronization: When
gameHandle.stop()orpause()is called, the engine usually handles audio suspension automatically, but developers can also override it manually through this Session. - Independence: This Session controls the engine's internal mixer and does not affect the host app's own native player audio.
Example Code
java
// 1. Get the audio session
SUDRTGameAudioSession audioSession = gameHandle.getGameAudioSession();
if (audioSession != null) {
// 2. Example: set the mini-game's global volume to 50%
audioSession.setVolume(0.5f);
// 3. Example: mute based on the app's business needs
if (isAppMuted) {
audioSession.setMute(true);
}
}Notes
- Null protection: Although the audio module is usually initialized after
SUDRTGameHandleis created successfully, in rare cases where engine initialization fails, this method may returnnull. - Timing recommendation: It is recommended to initialize audio parameters after
gameHandle.start(). - System conflicts: If the host app uses complex
AudioManagerfocus management logic, ensure that thisAudioSessionis operated in sync when requesting/releasing focus for the best user experience.
