Add support for audio ducking in plugin configuration #136
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🎵 Feature Overview
This PR introduces audio ducking functionality, allowing the plugin to lower the volume of other audio sources instead of completely stopping them when taking audio focus. This provides a much better user experience for apps that need to play audio alongside other media which are not supposed to be stopped.
🔧 Implementation Details
New
AudioFocusModeEnumUpdated Configuration API
Platform-Specific Implementations
iOS Implementation
NONE:AVAudioSession.Category.ambientEXCLUSIVE:AVAudioSession.Category.playbackDUCK:AVAudioSession.Category.playbackwith.duckOthersoptionAndroid Implementation
NONE:abandonAudioFocus()EXCLUSIVE:requestAudioFocus()withAUDIOFOCUS_GAINDUCK:requestAudioFocus()withAUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCKWeb Implementation
What Changed
The
focusboolean parameter inConfigureOptionshas been replaced withaudioFocusModeenum.Migration Guide
Impact Assessment
configure()- default behavior unchangedfocus: true/falseparameter🤔 Why Breaking Changes Were Necessary
1. Type Safety
The old boolean
focusparameter allowed invalid states and didn't prevent misconfigurations. The enum approach makes invalid states impossible at compile-time:2. Future Extensibility
The enum approach allows easy addition of new audio focus modes without API changes:
3. API Clarity
The enum makes the intent explicit and self-documenting:
4. Industry Standards
This approach aligns with platform conventions:
AUDIOFOCUS_GAIN,AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK)🎯 Most important use case
This feature allows e.g. a navigation app to play directions sounds and not stop playing music
🔄 Backward Compatibility Strategy
While this is technically a breaking change, the impact is minimal:
configure()work identicallyfocususageLimitations
Audio Session Management
The current implementation uses a resource-based session management approach where audio focus is only released when the last asset is unloaded. This design decision was made to balance simplicity with common usage patterns.
Current Behavior:
configure()call withEXCLUSIVEorDUCKmodes.preload()call.unload()Rationale:
This approach works well for the typical usage pattern of
preload() → play() → unload()but may not be optimal for all use cases.Alternative Session Management Strategies
When Current Approach May Not Be Ideal
Future Considerations
The current implementation prioritizes simplicity and covers the most common usage patterns. More sophisticated session management could be added in future versions based on community feedback and real-world usage requirements.
Let me know if you are interested in this Feature and if I should improve the session state management.