Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ would want.
a copy of the capture scripts.
- Scenes play sounds and music with `PlatformAudio`'s `playSound`,
`playMusic` and `stopMusic`, never a game's own wrappers of them.
- Keep `AudioManager`'s default of pausing the music while another app plays
audio (`stepsAsideForOtherAudio`); no game turns it off.
- The soundtrack follows only the game's own music switch: it mixes with
other apps' audio on every platform (ambient on Apple, no audio focus on
Android), and no game pauses it for other audio or calls.
- A game's soundtrack plays its intro once and then repeats a loop on the
beat: its masters stay out of the bundle, and the runtime's
`Tools/Audio/music.js` (its README), through the game's own two-line
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioAttributes;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
Expand All @@ -32,7 +30,7 @@
import java.io.ByteArrayOutputStream;

/**
* The device's part: haptics, audio focus, battery saver, the status bar,
* The device's part: haptics, battery saver, the status bar,
* the app's settings page, the battery and the connection, and re-encoding
* images between JPEG and PNG.
*
Expand All @@ -42,30 +40,23 @@
* and a short buzz on the oldest. Played as touch feedback, so the system's
* own "vibration & haptics" switch silences them as it does every app's.
*
* <p><b>Audio focus,</b> to the iOS build's ambient session: the player's
* own music wins. If something else is playing music as the game comes to
* the front, the game asks for no focus and keeps its soundtrack quiet
* (its sounds still mix in); otherwise it takes focus for the soundtrack and
* steps aside the moment another app takes it, and pauses for a call. It
* holds focus until it stops, not merely pauses ({@link AudioFocusPolicy}).
* Events: {@code audio} with {@code otherAudio} and {@code interrupted}.
* <p><b>No audio focus,</b> as the iOS build's ambient session: the game's
* soundtrack follows its own music switch alone and mixes with whatever else
* plays, the player's own music included, which plays on. Holding no focus,
* nothing (another app's playback, Android 12's fade-out) silences it.
*
* <p><b>Battery saver</b> is iOS's Low Power Mode: the game holds its frame
* rate to 60 while it is on. Event: {@code powerSave}.
*/
public final class DeviceServices implements AudioManager.OnAudioFocusChangeListener, PlatformService {
public final class DeviceServices implements PlatformService {
public static final String SERVICE = "device";

private final Activity activity;
private final PlatformServices services;
/** The status bar the game last asked for: null until it asks. UI thread. */
private Boolean statusBarVisible;
private final Vibrator vibrator;
private final AudioManager audio;
private final PowerManager power;
private AudioFocusRequest focusRequest;
private boolean hasFocus;
private final AudioFocusPolicy focusPolicy = new AudioFocusPolicy();

private final BroadcastReceiver powerSaveReceiver = new BroadcastReceiver() {
@Override
Expand All @@ -83,7 +74,6 @@ public DeviceServices(Activity activity, PlatformServices services) {
} else {
vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
}
audio = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
power = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
IntentFilter filter = new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
if (Build.VERSION.SDK_INT >= 33) {
Expand Down Expand Up @@ -259,30 +249,10 @@ private void showStatusBar(boolean visible) {
@Override
public void onResume() {
postPowerSave();
if (audio == null) return;
// Before SDL resumes the game's own stream, which is stopped only
// after a stop (`AudioFocusPolicy`).
switch (focusPolicy.onResume(hasFocus, audio.isMusicActive())) {
case OTHER_AUDIO:
services.post(SERVICE, "audio", PlatformServices.object("otherAudio", true, "interrupted", false));
break;
case REQUEST:
requestFocus();
break;
case KEEP:
break;
}
}

@Override
public void onStop() {
focusPolicy.onStop();
abandonFocus();
}

@Override
public void onDestroy() {
abandonFocus();
try {
activity.unregisterReceiver(powerSaveReceiver);
} catch (IllegalArgumentException alreadyGone) {
Expand All @@ -294,62 +264,6 @@ private void postPowerSave() {
services.post(SERVICE, "powerSave", PlatformServices.object("enabled", power != null && power.isPowerSaveMode()));
}

// MARK: - Audio focus

private void requestFocus() {
if (audio == null || hasFocus) return;
int result;
if (Build.VERSION.SDK_INT >= 26) {
if (focusRequest == null) {
focusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build())
.setOnAudioFocusChangeListener(this)
.build();
}
result = audio.requestAudioFocus(focusRequest);
} else {
result = audio.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
hasFocus = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
services.post(SERVICE, "audio", PlatformServices.object("otherAudio", !hasFocus, "interrupted", false));
}

private void abandonFocus() {
if (audio == null || !hasFocus) return;
if (Build.VERSION.SDK_INT >= 26 && focusRequest != null) {
audio.abandonAudioFocusRequest(focusRequest);
} else {
audio.abandonAudioFocus(this);
}
hasFocus = false;
}

@Override
public void onAudioFocusChange(int change) {
switch (change) {
case AudioManager.AUDIOFOCUS_GAIN:
hasFocus = true;
services.post(SERVICE, "audio", PlatformServices.object("otherAudio", false, "interrupted", false));
break;
case AudioManager.AUDIOFOCUS_LOSS:
// Another app's music: the soundtrack steps aside for good,
// until the game next comes to the front.
abandonFocus();
services.post(SERVICE, "audio", PlatformServices.object("otherAudio", true, "interrupted", false));
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
// A call or an alarm: everything waits.
services.post(SERVICE, "audio", PlatformServices.object("otherAudio", false, "interrupted", true));
break;
default:
// LOSS_TRANSIENT_CAN_DUCK: the system ducks us itself.
break;
}
}

// MARK: - Haptics

private void haptic(String kind) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ public enum AndroidPlatformServices {
/// Purchases, made whether or not there's a channel.
public var billing: Billing?

/// Told when audio focus changes (the `audio` event): whether other
/// audio is playing, and whether a call interrupted the game.
public var audioFocusChanged: ((_ otherAudio: Bool, _ interrupted: Bool) -> Void)?
/// The settings' status bar switch, shown or hidden as it changes.
public var statusBar: StatusBar?
/// The battery and connection a game's diagnostics read
Expand Down Expand Up @@ -182,7 +179,6 @@ public enum AndroidPlatformServices {
}
}
let center = configuration.notificationCenter
let audioFocusChanged = configuration.audioFocusChanged
channel.on("device") { event, body in
switch event {
case "powerSave":
Expand All @@ -191,8 +187,6 @@ public enum AndroidPlatformServices {
Device.lowPowerMode = enabled
// As iOS posts Low Power Mode changing.
center.post(name: .NSProcessInfoPowerStateDidChange, object: nil)
case "audio":
audioFocusChanged?(body.bool("otherAudio") ?? false, body.bool("interrupted") ?? false)
default:
break
}
Expand Down
11 changes: 2 additions & 9 deletions Platform/Sources/PlatformAndroidHost/ToolkitServices.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#if SPRITEKIT_SHIM
import Foundation
import PlatformAndroid
import PlatformAudio
import PlatformDisplay
import PlatformGameCenter
import PlatformShim
Expand All @@ -14,8 +13,8 @@ extension AndroidPlatformServices.Configuration {
/// The toolkit's managers on Google Play and the device, as most games
/// hand them: Play Games' achievements and leaderboards to
/// `gameCenter`, Saved Games under `ICloudSyncManager` (`syncsSavedData`),
/// Play Billing to `store` for its configured products, and audio focus
/// to `audio`, and with `statusBar` the settings' switch
/// Play Billing to `store` for its configured products, and with
/// `statusBar` the settings' switch
/// (`StatusBarSetting`) to the status bar. A manager left nil gets
/// nothing. Set anything else (the player for GameKit's
/// `GKLocalPlayer`, diagnostics) on the result before
Expand All @@ -24,7 +23,6 @@ extension AndroidPlatformServices.Configuration {
gameCenter: GameCenterManager?,
syncsSavedData: Bool = true,
store: StoreManager? = nil,
audio: AudioManager? = .shared,
statusBar: Bool = false
) -> Self {
var services = Self()
Expand All @@ -38,11 +36,6 @@ extension AndroidPlatformServices.Configuration {
if let store {
services.billing = .init(store: store)
}
if let audio {
services.audioFocusChanged = { otherAudio, interrupted in
MainActor.assumeIsolated { audio.audioFocusDidChange(otherAudio: otherAudio, interrupted: interrupted) }
}
}
if statusBar {
services.statusBar = .setting
}
Expand Down
Loading
Loading