Skip to content

Update Media Element to use Media3#2076

Merged
TheCodeTraveler merged 137 commits intoCommunityToolkit:mainfrom
ne0rrmatrix:Media3
Jan 14, 2025
Merged

Update Media Element to use Media3#2076
TheCodeTraveler merged 137 commits intoCommunityToolkit:mainfrom
ne0rrmatrix:Media3

Conversation

@ne0rrmatrix
Copy link
Copy Markdown
Member

@ne0rrmatrix ne0rrmatrix commented Jul 31, 2024

Description of Change

Port Media Element to use Media 3 Binding from Xamarin AndroidX team. This will allow us to stop using deprecated methods and classes and move forward with adding new features to this media player.

Linked Issues

PR Checklist

Additional information

This is ready for testing and I encourage ppl to test and report any issues below. It is not yet ready for dotnet 9.x and will be updated as soon as toolkit main is updated to add support.

Updated `CommunityToolkit.Maui.MediaElement.csproj` to replace `Xam.Plugins.Android.ExoPlayer` with `Xamarin.AndroidX.Media3` packages.

In `MediaControlsService.android.cs`, updated drawable resources for media control actions to new icons.

Replaced `StyledPlayerView` with `PlayerView` in `MauiMediaElement.android.cs`.

Refactored `MediaManager.android.cs` to use `AndroidX.Media3` classes and interfaces, updating method signatures and interface implementations.

Updated `MediaManager.shared.cs` to use `AndroidX.Media3.ExoPlayer.IExoPlayer` for Android.

Aligned methods and properties with the new Media3 API, including playback state handling, error handling, and media session management.

Refactored `OnPlaybackStateChanged` and `OnPlayerStateChanged` methods to handle new playback state values.

Modified `InitializeMediaSession` to comment out `MediaSessionConnector` and adjust media session setup.

Ensured compatibility with the new Media3 library across various methods.
Updated `CommunityToolkit.Maui.Camera.csproj` and `CommunityToolkit.Maui.MediaElement.csproj` to use newer versions of several Xamarin.AndroidX packages and added new package references.

Refactored `MediaManager.android.cs`:
- Removed unused imports and added new ones.
- Replaced `MediaControllerCompat` and `MediaSessionCompat` with `MediaSession`.
- Updated `OnPlaybackStateChanged` to use `PlaybackStateCompat` constants.
- Removed `BroadcastUpdate` method and related calls.
- Introduced `SetPlayerData` method for setting media metadata.
- Removed `InitializeMediaSession` method and related calls.
- Updated `StartService` to use the new `MediaSession`.
- Removed `UIUpdateReceiver` class and related calls.
- Updated method signatures to use new types from updated AndroidX libraries.
@TheCodeTraveler TheCodeTraveler added the needs discussion Discuss it on the next Monthly standup label Jul 31, 2024
Removed AudioManager and related methods from MediaControlsService.
Replaced Trace.WriteLine with Trace.TraceError for error logging.
Removed OnSetupAudioServices method and its calls.
Modified InitializeNotification to remove SetShowActionsInCompactView.
Refactored OnSetContent and OnSetIntents methods.
Added UIUpdateReceiver class to handle updates from MediaControlsService.
Registered and unregistered UIUpdateReceiver in MediaManager.
Added BroadcastUpdate methods for play and pause actions.
Updated SetPlayerData to return MediaItem.Builder.
Aligned method signatures with new MediaItem.Builder approach.
Handled ACTION_PLAY, ACTION_PAUSE, ACTION_FASTFORWARD, and ACTION_REWIND in UIUpdateReceiver.
Updated method names and parameters to match AndroidX Media3 conventions.
Removed redundant or obsolete code and comments.
Updated `CommunityToolkit.Maui.Camera.csproj` and `CommunityToolkit.Maui.MediaElement.csproj`:
- Removed `Xamarin.AndroidX.Lifecycle.LiveData`.
- Downgraded several `Xamarin.AndroidX.Lifecycle` packages from `2.8.4` to `2.8.3.1`.
- Downgraded `Xamarin.AndroidX.Activity.Ktx` from `1.9.1` to `1.9.0.4`.
- Corrected `Xamarin.AndroidX.Media3.Ui` to `Xamarin.AndroidX.Media3.UI`.

Enhanced `MediaManager.android.cs`:
- Added `mediaSession` field to `MediaManager` class.
- Updated `SetPlayerData` to set media ID using `mediaItem.SetMediaId(url)`.
- Updated method signatures to use fully qualified names for `global::AndroidX.Media3.Common` types.
Refactored MediaManager.android.cs for improved readability and maintainability. Updated package references in CommunityToolkit.Maui.MediaElement.csproj, including the removal of Xamarin.AndroidX.Media3.Transformer and addition of new ExoPlayer packages. Enhanced method implementations and ensured compatibility with updated packages.
@ne0rrmatrix ne0rrmatrix self-assigned this Aug 3, 2024
@ne0rrmatrix ne0rrmatrix added the 📽️ MediaElement Issue/PR that has to do with MediaElement label Aug 3, 2024
Refactored `PlatformPause`, `PlatformSeek`, `PlatformStop`, and `PlatformUpdateSource` methods to streamline code and reduce potential issues:
- Removed TODO comment in `PlatformPause`.
- Simplified `PlatformSeek` by removing listener management and async handling.
- Changed `PlatformStop` to a synchronous method, ensuring proper player stop without async seek.
- Updated `PlatformUpdateSource` to set media items directly without modifying the listener.
ne0rrmatrix and others added 11 commits August 3, 2024 19:11
Updated .NET to `net8.0` and Maui to `8.0.70` in `Directory.Build.props`. Removed old `Xamarin.AndroidX` packages and added `Xamarin.AndroidX.Collection.Jvm` and `Xamarin.AndroidX.Activity.Ktx` in `CommunityToolkit.Maui.MediaElement.csproj`.

Introduced constants for playback states (`sTATE_IDLE`, `sTATE_BUFFERING`, `sTATE_READY`, `sTATE_ENDED`) and replaced `PlaybackStateCompat.StatePlaying` with `sTATE_READY` in `MediaManager.android.cs`. Added `Player.SetHandleAudioBecomingNoisy(true)` for handling audio interruptions and a `UIUpdateReceiver` for UI updates based on player state changes.

Improved error handling in `PlatformSeek` method to avoid exceptions and ensure execution on the calling thread. Initialized `MediaItem.Builder` in `SetPlayerData` method. Updated `PlatformUpdateAspect` and `PlatformUpdateShouldLoopPlayback` methods to set properties based on `MediaElement` attributes.
* Modified `PlatformStop` to reset media position to start (0) and explicitly set `MediaElement.Position` to `TimeSpan.Zero`.
* Added `MediaSession` release and disposal.
Copy link
Copy Markdown
Contributor

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.

Copilot reviewed 6 out of 16 changed files in this pull request and generated no comments.

Files not reviewed (10)
  • samples/CommunityToolkit.Maui.Sample/Pages/Views/MediaElement/MediaElementCarouselViewPage.xaml: Language not supported
  • samples/CommunityToolkit.Maui.Sample/Pages/Views/MediaElement/MediaElementCollectionViewPage.xaml: Language not supported
  • samples/CommunityToolkit.Maui.Sample/Platforms/Android/AndroidManifest.xml: Language not supported
  • src/CommunityToolkit.Maui.MediaElement/CommunityToolkit.Maui.MediaElement.csproj: Language not supported
  • src/CommunityToolkit.Maui.MediaElement/Primitives/AndroidMediaPermissions.android.cs: Evaluated as low risk
  • src/CommunityToolkit.Maui.MediaElement/Handlers/MediaElementHandler.android.cs: Evaluated as low risk
  • src/CommunityToolkit.Maui.MediaElement/Primitives/Metadata.macios.cs: Evaluated as low risk
  • src/CommunityToolkit.Maui.MediaElement/Primitives/Metadata.windows.cs: Evaluated as low risk
  • src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.shared.cs: Evaluated as low risk
  • src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.android.cs: Evaluated as low risk

@ne0rrmatrix ne0rrmatrix added the pending documentation This feature requires documentation label Jan 13, 2025
@TheCodeTraveler TheCodeTraveler removed pending documentation This feature requires documentation needs discussion Discuss it on the next Monthly standup labels Jan 13, 2025
@TheCodeTraveler TheCodeTraveler merged commit 7439c34 into CommunityToolkit:main Jan 14, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

breaking change This label is used for PRs that include a breaking change 📽️ MediaElement Issue/PR that has to do with MediaElement

Projects

None yet

9 participants