Skip to content
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

Update Media Element to use Media3 #2076

Open
wants to merge 121 commits into
base: main
Choose a base branch
from

Conversation

ne0rrmatrix
Copy link
Contributor

@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

  • 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

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.
@brminnick brminnick 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.
@ne0rrmatrix ne0rrmatrix added the needs discussion Discuss it on the next Monthly standup label Dec 5, 2024
Updated GetBytesFromUrl and CreateMediaItem methods to use ConfigureAwait(false) for async calls to avoid capturing the synchronization context. Replaced MediaSession.Builder class in PlaybackState.StateSkippingToQueueItem with the correct usage.
Refactored BoundServiceConnection to be a partial sealed class and added an IsConnected property. Updated OnServiceConnected to call UpdateSource instead of UpdatePlayer. Removed UpdatePlayer method from MediaManager. Made formatting and consistency improvements.
Removed Activity?.UpdateSource() call in IServiceConnection.OnServiceConnected.
Renamed StartConnection to StartService in MediaManager.android.cs.
Updated PlatformUpdateSource to call StartService if connection is null.
The startServiceSourceToken field of type CancellationTokenSource has been removed from the MediaManager class. The line that disposes of startServiceSourceToken in the Dispose method has also been removed.
- Modify `BoundServiceConnection` to update notifications on service connection.
- Change `MediaControlsService` to a partial class and update `Player` property type.
- Refactor `MauiMediaElement` for readability and maintainability.
- Add `UpdateNotifications` method to `MediaManager`.
- Improve `PlatformUpdateSource` method in `MediaManager`.
@ne0rrmatrix
Copy link
Contributor Author

The blocker is removed. Updating Android workload will now allow you to build using debug with: https://github.com/dotnet/android/releases/tag/35.0.24

> dotnet workload install android
...
Successfully installed workload(s) android.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 7 out of 22 changed files in this pull request and generated no suggestions.

Files not reviewed (15)
  • 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.Camera/CommunityToolkit.Maui.Camera.csproj: 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/Primitives/Metadata.macios.cs: Evaluated as low risk
  • src/CommunityToolkit.Maui.MediaElement/Primitives/PlayerViewChangedEventArgs.android.cs: Evaluated as low risk
  • src/CommunityToolkit.Maui.MediaElement/Primitives/MediaFailedEventArgs.cs: Evaluated as low risk
  • src/CommunityToolkit.Maui.MediaElement/Primitives/MediaPositionChangedEventArgs.cs: Evaluated as low risk
  • src/CommunityToolkit.Maui.MediaElement/Primitives/MediaStateChangedEventArgs.cs: Evaluated as low risk
  • src/CommunityToolkit.Maui.MediaElement/AppBuilderExtensions.shared.cs: Evaluated as low risk
  • src/CommunityToolkit.Maui.MediaElement/Primitives/Metadata.windows.cs: Evaluated as low risk
  • src/CommunityToolkit.Maui.MediaElement/Handlers/MediaElementHandler.android.cs: Evaluated as low risk
  • src/CommunityToolkit.Maui.MediaElement/Primitives/SeekRequestedEventArgs.cs: Evaluated as low risk
Comments skipped due to low confidence (5)

src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.android.cs:116

  • [nitpick] The variable name p0 is ambiguous. It should be renamed to isFullScreen.
// `p0` is the boolean value of isFullScreen being passed into the method.

src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.android.cs:113

  • The error message should be "PlayerView cannot be null when the FullScreen button is tapped" to match the actual variable name.
throw new InvalidOperationException("UpdatedPlayerView cannot be null when the FullScreen button is tapped");

src/CommunityToolkit.Maui.MediaElement/Services/MediaControlsService.android.cs:70

  • Ensure that NotificationManager and playerNotificationManager are not null before calling their Dispose methods.
NotificationManager?.Dispose();

src/CommunityToolkit.Maui.MediaElement/Services/BoundServiceConnection.android.cs:20

  • Add a null check for Activity before calling UpdateNotifications to prevent potential null reference exceptions.
Activity?.UpdateNotifications();

src/CommunityToolkit.Maui.MediaElement/Services/BoundServiceBinder.android.cs:6

  • [nitpick] The class name 'BoundServiceBinder' is ambiguous. It should be renamed to something more descriptive like 'MediaControlsServiceBinder'.
sealed class BoundServiceBinder(MediaControlsService mediaControlsService) : Binder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change This label is used for PRs that include a breaking change 📽️ MediaElement Issue/PR that has to do with MediaElement needs discussion Discuss it on the next Monthly standup
Projects
None yet
7 participants