Skip to content

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:

  1. Global volume adjustment: Adjust the mixed volume output by the mini-game internally without changing the system media volume.
  2. 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).
  3. 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 ​

  1. Singleton binding: Each SUDRTGameHandle instance corresponds to a unique AudioSession.
  2. Lifecycle synchronization: When gameHandle.stop() or pause() is called, the engine usually handles audio suspension automatically, but developers can also override it manually through this Session.
  3. 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 SUDRTGameHandle is created successfully, in rare cases where engine initialization fails, this method may return null.
  • Timing recommendation: It is recommended to initialize audio parameters after gameHandle.start().
  • System conflicts: If the host app uses complex AudioManager focus management logic, ensure that this AudioSession is operated in sync when requesting/releasing focus for the best user experience.