Issue Description:
Audio becomes silent or fails to start when the app moves between foreground and background states during or after joining a channel. In some cases, playback and capture both stop unexpectedly on iOS devices.
Platform/SDK:
iOS and Android (Agora Audio SDK)
Error Message:
- iOS: AudioDeviceMalfunctionCheck: invalid flags: audiosession_config_failed_ 1
- iOS: CallbackOnError errCode 561017449 / 1206
- Android: OpenSL failed to start audio device in the background
Root Cause:
These issues are related to how the app manages the audio module (ADM) when switching between the foreground and background.
- Case 1 (iOS): The app joined a channel while in the background. The audio device module (ADM) failed to start due to an invalid audio session configuration. When returning to the foreground, the audio system detected the malfunction but was unable to successfully restart.
- Case 2 (Android): The user joined the channel directly from the background, where OpenSL (the Android audio engine) cannot start the recording module. This behavior is expected; audio capture in the background is restricted.
- Case 3 (iOS): The app encountered a system-level interruption that stopped the audio unit. When the interruption ended and ADM tried to restart, initialization failed, resulting in both playback and capture dropping to zero.
Solution:
- Avoid joining channels or initialising audio while the app is in the background. Always ensure the app is in the foreground before starting audio capture or playback.
- Handle system interruptions properly on iOS by listening for interruption callbacks. When an interruption ends, explicitly restart the audio device or re-enable local audio.
- Implement ADM restart logic in case of audio device malfunction. Detect
OnErrororAudioDeviceMalfunctionCheckfailures and invoke a controlled reinitialization of the audio engine. - For Android: If users move the app to the background before audio starts, the SDK will not begin recording until the app returns to the foreground. Ensure normal capture resumes when the activity becomes active again.
- Keep the SDK updated to the latest version, which includes improved audio session recovery mechanisms.
Following these practices ensures stable audio behaviour during app state transitions and prevents no-sound scenarios.