Skip to content

Conversation

ne0rrmatrix
Copy link
Contributor

Description of Change

Switch from using default service implementation to using the recommended by Google. Player and MediaSession in MediaSessionService with MediaController in Activity. The deprecated API's for notifications have been completely removed and are no longer needed.

No change in features with respect to media notifications. The rich media notifications have been significantly overhauled and following best principles. The issue with notification getting stuck and not being removed when leaving page are fixed. When you stop the player the notifications will be removed automatically. If you navigate away from page the notifications will also be removed. It you force close the app by swiping up it will close the notifications and end playback.

The new changes include using a MediaController as an interface to the MediaSessionService which lives in the Activity and fully replaces the player functionality as far as MediaManager is concerned.

Deprecated API's like PlayerNotifications and custom http handlers to make web requests to get artwork have been removed and replaced with native methods that do the same thing. The Media Notifications are now unified under a single class and none of the API's are deprecated. This has backwards support and is tested from API 26 to 35.

Breaking Changes:

  • Applies to Android Version!
  • Should apply to all devices

The downside to this is possible a deal breaker and is a breaking change. Currently MediaElement supports as many concurrent players as you the developers chooses to use. With this change it will not longer support this. It will only support a single instance per app. With how the service model and integration between the player, player view and mediaSession works it is not really possible to have more than one Rich Media Session at the same time. Imagine your notification tray having a new item for every video in your collection?

This is a big issue. It is an ongoing issue for Windows, MacOS and iOS. All 3 of those devices have current and ongoing issues with CollectionViews, Grids, and Carousel Views. I do not believe it should ever have been intended to have this functionality and we should work towards creating sample pages where developers can effectively use those views and have a media element. But we need to keep to a single, reusable instance. Having multiple simultaneous players should not be supported and is not needed if the pages are designed correctly.

  • Fixes #

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard
  • Documentation created or updated: https://github.com/MicrosoftDocs/CommunityToolkit/pulls

Additional information

Refactored `MediaElement` implementation for Android to replace the bound service (`MediaControlsService`) with `MediaSession` and `MediaController`, simplifying the architecture and aligning with modern Android media playback practices.

- Removed `BoundServiceBinder` and `BoundServiceConnection` classes.
- Updated `MediaControlsService` to use `ExoPlayer` and `MediaSession`.
- Simplified notification management with `NotificationCompat`.
- Refactored `MediaManager` to remove session and bound service logic.
- Added `CreateMediaController` for asynchronous `MediaController` setup.
- Updated `MauiMediaElement` to support `MediaController` integration.
- Removed legacy code, unused imports, and redundant methods.
…gating a second time to same item and now the image does display in notifications. It did not previously. This affects load times. It is now faster.
@ne0rrmatrix ne0rrmatrix added the needs discussion Discuss it on the next Monthly standup label Oct 2, 2025
- Updated `textureview.xml` to use black backgrounds for better consistency.
- Replaced `RelativeLayout` with `FrameLayout` in `MauiMediaElement.android.cs` for improved layout management.
- Refactored `MediaControlsService.android.cs`:
  - Replaced `static readonly` fields with `const` for immutability.
  - Added audio attributes for better playback handling.
  - Updated track selector to fallback to system language.
  - Added handling for audio interruptions (e.g., unplugging headphones).
- Added `OnTracksChanged` in `MediaManager.android.cs` to manage subtitle button visibility.
- Removed unused imports and redundant constants for cleaner code.
- Improved buffering strategies for smoother playback.
@ne0rrmatrix
Copy link
Contributor Author

The issue with transparent or white background has been fixed! So this is now ready for review :)

@ne0rrmatrix ne0rrmatrix marked this pull request as ready for review October 2, 2025 21:41
@Copilot Copilot AI review requested due to automatic review settings October 2, 2025 21:41
Copy link
Contributor

@Copilot Copilot AI left a 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 modernizes the Android MediaElement implementation by replacing the deprecated service architecture with Google's recommended MediaSessionService approach. The changes migrate from using ExoPlayer directly to using MediaController with MediaSessionService, implementing proper lifecycle management and unified media notifications.

Key changes include:

  • Replaced ExoPlayer with MediaController as the platform media element
  • Implemented MediaSessionService pattern for background media playback
  • Unified media notifications using native Android methods instead of custom implementations

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
MediaManager.shared.cs Updated global using alias from IExoPlayer to MediaController
MediaManager.android.cs Complete overhaul replacing ExoPlayer with MediaController, removed service binding logic
MauiMediaElement.android.cs Refactored layout from RelativeLayout to FrameLayout, added MediaController integration
MediaControlsService.android.cs Converted from Service to MediaSessionService with proper notification handling
BoundServiceConnection.android.cs File deleted - service binding no longer needed
BoundServiceBinder.android.cs File deleted - service binding no longer needed
textureview.xml Changed background colors from white/transparent to black
MediaElementHandler.android.cs Added async MediaController creation and connection logic
AppBuilderExtensions.shared.cs Removed MediaControlsService dependency injection

ne0rrmatrix and others added 7 commits October 2, 2025 14:43
Updated the CreateMediaController method to include an optional
CancellationToken parameter, allowing the operation to be canceled
if needed. Modified the WaitAsync call to respect the provided
cancellation token, improving the method's flexibility and
robustness.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs discussion Discuss it on the next Monthly standup

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants