-
-
Notifications
You must be signed in to change notification settings - Fork 60
Add configurable audio focus and interruption handling for Android and iOS #193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: jfversluis <[email protected]>
Co-authored-by: jfversluis <[email protected]>
Co-authored-by: jfversluis <[email protected]>
|
@copilot can we make this behavior configurable through an option? |
Co-authored-by: jfversluis <[email protected]>
Yes! I've made the behavior configurable. Added: Android: Example usage: var audioPlayer = audioManager.CreatePlayer(
await FileSystem.OpenAppPackageFileAsync("audio.mp3"),
new AudioPlayerOptions
{
#if ANDROID
ManageAudioFocus = false // Disable on Android
#elif IOS || MACCATALYST
HandleAudioInterruptions = false // Disable on iOS/macOS
#endif
});Both default to Commit: 42ae44f |
|
@copilot build errors: D:\a\Plugin.Maui.Audio\Plugin.Maui.Audio\src\Plugin.Maui.Audio\AudioPlayer\AudioPlayer.macios.cs(275,102): error CS0117: 'AVAudioSession' does not contain a definition for 'InterruptionTypeKey' [D:\a\Plugin.Maui.Audio\Plugin.Maui.Audio\src\Plugin.Maui.Audio\Plugin.Maui.Audio.csproj::TargetFramework=net9.0-maccatalyst] |
…udioManager interface Co-authored-by: jfversluis <[email protected]>
Fixed the build errors: iOS/macOS: Used string literal keys Android: Fully qualified the interface as Commit: 01684a9 |
|
@copilot more build errors D:\a\Plugin.Maui.Audio\Plugin.Maui.Audio\src\Plugin.Maui.Audio\AudioPlayer\AudioPlayer.android.cs(197,19): error CS0030: Cannot convert type 'Java.Lang.Object' to 'Plugin.Maui.Audio.AudioManager' [D:\a\Plugin.Maui.Audio\Plugin.Maui.Audio\src\Plugin.Maui.Audio\Plugin.Maui.Audio.csproj::TargetFramework=net9.0-android] |
Co-authored-by: jfversluis <[email protected]>
Fixed the naming conflict. Changed all Updated:
Commit: 20bee77 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements automatic audio focus handling for Android and iOS to properly manage audio playback during phone calls and other audio interruptions. The implementation follows platform best practices and provides developers with configurable options to control the behavior.
Key Changes:
- Added automatic audio focus management for Android with configurable
ManageAudioFocusoption - Added automatic audio interruption handling for iOS/macOS with configurable
HandleAudioInterruptionsoption - Both features are enabled by default but can be disabled for manual control
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| AudioPlayerOptions.android.cs | Added ManageAudioFocus property to enable/disable automatic audio focus management |
| AudioPlayerOptions.macios.cs | Added HandleAudioInterruptions property to enable/disable automatic interruption handling |
| AudioPlayer.android.cs | Implemented audio focus request/abandon logic and focus change listener |
| AudioPlayer.macios.cs | Implemented audio session interruption notification handling |
| audio-player.md | Documented the new audio focus and interruption handling features |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot address comments |
…estoration Co-authored-by: jfversluis <[email protected]>
Addressed all code review comments: iOS/macOS:
Android:
Commit: 53ad368 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot address comments |
… behavior Co-authored-by: jfversluis <[email protected]>
Addressed code review comments: iOS/macOS:
Android:
Note: The Commit: e653d18 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot address comments |
…l helper Co-authored-by: jfversluis <[email protected]>
Addressed all code review comments: Android:
iOS/macOS:
Commit: 2d67a19 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@copilot address comments |
Co-authored-by: jfversluis <[email protected]>
Addressed documentation comments: Android:
iOS/macOS:
Note: The Commit: d03d365 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Implementing audio focus handling for Android and iOS to pause audio during phone calls and other audio interruptions.
Plan:
Recent Changes:
Fourth Round Code Review Improvements:
Android (AudioPlayer.android.cs):
AudioFocusChangeListenerclass explaining its purposeiOS/macOS (AudioPlayer.macios.cs):
GetInterruptionType()method explaining parameter, return value, and default behaviorGetInterruptionOptions()method explaining parameter and return valueNote: Regarding the
RequestAudioFocus()return value suggestion - the current behavior of returningfalsewhen disabled is intentional. The caller uses this to log a warning but continues playing for backward compatibility. Changing it to returntruewhen disabled would be misleading as it would imply focus was successfully acquired.Previous Code Review Improvements:
Third Round:
PlayInternal()helper, added clarifying commentsPlay()method for consistent state managementSecond Round:
GetInterruptionType()explaining default behaviorPauseInternal()helper method to eliminate code duplicationFirst Round:
GetInterruptionType()andGetInterruptionOptions()helper methodsDuckingVolumeMultiplierconstant, fixed volume restoration logicBuild Error Fixes:
AudioManagerreferences toAndroid.Media.AudioManagerIOnAudioFocusChangeListenerinterfaceConfiguration Options:
Android (
AudioPlayerOptions.android.cs):ManageAudioFocusproperty (default:true)true: Automatically requests/abandons audio focus and responds to focus changesfalse: No audio focus management, giving developers full manual controliOS/macOS (
AudioPlayerOptions.macios.cs):HandleAudioInterruptionsproperty (default:true)true: Automatically pauses/resumes on interruptionsfalse: No automatic interruption handling, giving developers full manual controlAndroid Changes (AudioPlayer.android.cs):
✅ Added Android.Media.AudioManager and AudioFocusRequestClass fields for managing audio focus
✅ Implemented audio focus request/abandon using both modern (API 26+) and legacy APIs
✅ Created AudioFocusChangeListener to handle focus changes
✅ Play() now requests audio focus before starting (if enabled)
✅ Pause() and Stop() now abandon audio focus (if enabled)
✅ Proper cleanup in Dispose()
✅ Audio focus only initialized and used if
ManageAudioFocus = true✅ Improved volume ducking with proper restoration
✅ Extracted PauseInternal() and PlayInternal() helpers to eliminate code duplication
✅ Comprehensive XML documentation
iOS Changes (AudioPlayer.macios.cs):
✅ Added NSObject interruptionObserver for AVAudioSession interruption notifications
✅ Registered for AVAudioSession.InterruptionNotification in PreparePlayer()
✅ Implemented HandleAudioSessionInterruption() to respond to interruptions
✅ Proper cleanup of observers in Dispose()
✅ Updated SetSource() to re-register interruption observers
✅ Interruptions only registered if
HandleAudioInterruptions = true✅ Safer casting with helper methods
✅ Uses public Play() method for consistent state management
✅ Comprehensive XML documentation
Documentation:
✅ Added new section "Audio Focus and Interruption Handling" in docs/audio-player.md
✅ Clarified "audio ducking" terminology
✅ Explained that behaviors are automatic but manual control is still available
✅ Added note about backward compatibility
✅ Links to official platform documentation
✅ Added configuration examples for both Android and iOS
✅ Documented the new
ManageAudioFocusandHandleAudioInterruptionsoptionsThis implementation follows the official Android Audio Focus and iOS Audio Interruption guidelines and provides developers with full control over the behavior.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.