Add support for audio ducking in plugin configuration#136
Conversation
|
I guess this PR is related or might even fix these two issues: |
|
I just wanted to ask if you are interested in my suggested feature. It looks like the maintainer bazuka5801 is not active anymore since his GitHub activity is Thanks in advance. |
|
Hey, I just wanted to kindly follow up on this pull request. It has been open for quite some time, and it seems like it could be a valuable improvement for the project. Would it be possible to review and potentially merge it when you have time? I think several users would benefit from having these changes included. Thanks again for maintaining the project and for all your work! |
|
I wanted to drop a comment to highlight how important this feature is for the community. The current limitations without this fix/feature make it really difficult to build robust audio experiences in Capacitor. This PR solves a major pain point. It would be awesome if a maintainer could take a look and get this merged. Big +1 from my side! 👍 |
🎵 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.