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 95 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 currently not ready for review or testing yet. It should be ready in a day or two. ATM it works fine in Android 33/34 but I have to test and then port the new controls to older API's over the next few days.

Also camera nuget needs to tested and verified the package updates I added did not break anything. Had to be done for Media Element to compile.

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
Copy link
Contributor Author

This PR is ready for review. The requested changes including a fix for the issue with foreground service startup and crash on null Intent has been fixed. The changes made to other packages have been removed and are no longer needed after latest update last week. The media 3 packages are now aligned with current version of media 3 and are stable.

@KeithBoynton
Copy link

Keen to see this reviewed and merged so I can get along with contributing some Android playlist support

Comment on lines -56 to -70

<!-- Xamarin.AndroidX.Camera 1.3.1.1 depends on Xamarin.AndroidX.Lifecycle.LiveData 2.7.0.1:
https://www.nuget.org/packages/Xamarin.AndroidX.Camera.Core/#dependencies-body-tab
However, Dotnet MAUI still implicitly references Xamarin.AndroidX.Lifecycle.LiveData 2.6.1.3:
https://github.com/dotnet/maui/blob/main/eng/AndroidX.targets
This causes build error "Detected package downgrade":
https://learn.microsoft.com/en-us/nuget/reference/errors-and-warnings/nu1605
Workaround: add a package reference to the higher version of the package. Possibly can be removed when MAUI bumps the implicit reference version.
-->
<PackageReference Include="Xamarin.AndroidX.Lifecycle.LiveData" Version="2.7.0.1" />

<!--Adding the above package reference requires Workaround for https://github.com/xamarin/AndroidX/issues/800#issuecomment-1774639627 -->
<!-- Tracking issue https://github.com/xamarin/AndroidX/issues/764 -->
<PackageReference Include="Xamarin.AndroidX.Collection" Version="1.4.0.1" />
<PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.4.0.1" />
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's move this to a separate PR since it not related to CommunityToolkit.Maui.MediaElement

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will try reverting the changes. It was only modified so that transient dependancies did not clash

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just looked at the code and was unable to find the referenced code. It has been removed. Not sure when. Can you try updating your local source.

@KeithBoynton
Copy link

Hi @brminnick any joy with progressing the review of this? Getting this merged would really help me with some playlist related work I need to do on the Media Element. It looks like James responded to your comments

@brminnick
Copy link
Collaborator

@KeithBoynton My current focus is preparing for .NET 9

@KeithBoynton
Copy link

@brminnick If there's not much left outstanding on the review, it really would help unblock so we can work on the Media Element while you're focussing on .NET 9

Copy link
Member

@pictos pictos left a comment

Choose a reason for hiding this comment

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

Just a small comment. I believe this is really close to be merged

pictos

This comment was marked as spam.

@pictos
Copy link
Member

pictos commented Oct 31, 2024

@ne0rrmatrix, is this like ready to go? IF so I can give it try on my end. But I just have an emulator for now, I believe it will work

@ne0rrmatrix
Copy link
Contributor Author

@ne0rrmatrix, is this like ready to go? IF so I can give it try on my end. But I just have an emulator for now, I believe it will work

Gimme 30 min. I am just finishing the recommended changes you requested. I found another case of private set. I am updating and testing those changes. Once that is done it should be good to go.

@ne0rrmatrix
Copy link
Contributor Author

Changes done! Ready for you to test in emulator @pictos

@brminnick brminnick removed the needs discussion Discuss it on the next Monthly standup label Nov 7, 2024
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
Projects
None yet
7 participants