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 87 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

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.
…ediaManager inherited from service. This refactoring fixes almost every issue.
Updated MediaElement autoplay properties to False in XAML files.
Refactored MediaControlsService to use public NotificationManager.
Added id parameter to handle unique notifications in various methods.
Ensured proper disposal of resources in Dispose methods.
Updated methods to conditionally stop foreground services based on Android version.
Enhanced notification handling and creation with unique ids.
Improved resource management and connection handling in MediaManager.
Improved null safety by adding `[MemberNotNull]` attributes and using `ArgumentNullException.ThrowIfNull` in various methods. Refactored `UpdateNotifications` to streamline null checks and directly assign values. Added `UpdatePlayer` method to ensure `connection` and `PlayerView` are not null and set `PlayerView.DefaultArtwork`. Modified `PlaybackState.StateSkippingToQueueItem` to remove redundant notification updates. Updated `PlatformUpdateSource` to call `UpdateNotifications` when the source is set without errors. Ensured `mediaItem` is not null in `StopService` method.
Removed `id` parameter from various methods in `MediaControlsService`
and `MediaManager`. Notifications and channels now use a fixed ID "1".
Updated `OnTaskRemoved` to stop and release the `Player`.
@Pmr-precure
Copy link

Since the Android foreground service was introduced we have had this issue:
368992584-8b451775-7c57-40fe-9e78-2c67905c65ec

This PR has fixed that!

@ne0rrmatrix ne0rrmatrix marked this pull request as ready for review September 24, 2024 15:22
@ne0rrmatrix ne0rrmatrix requested a review from a team September 24, 2024 15:22
Removed `System.Runtime.InteropServices` and `Microsoft.Win32.SafeHandles` using directives from `MediaControlsService.android.cs` as they are no longer referenced in the code.
…tion back if it is cancelled when playback rersumes. Moved notification into on create.
Revert changes to Camera
@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

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