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

Lock Screen Controls and Metadata Support for Windows, Android, IOS and Mac Catalyst #1782

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

Conversation

ne0rrmatrix
Copy link
Contributor

@ne0rrmatrix ne0rrmatrix commented Mar 31, 2024

  • Feature/Proposal

Add Support for Lock Screen Controls and Metadata.

Description of Change

Add Lock Screen Media Controls to Windows, Android, iOS, and Mac Catalyst. Metadata like Title, Artist, and Album art will be displayed for playing media.

Linked Issues

PR Checklist

Additional information

Add support for Lock Screen Controls to Windows, Mac, IOS, and Android. Support for adding Metadata in XAML or code behind.

Example usage in XAML:

 <toolkit:MediaElement
     x:Name="MediaElement"
     WidthRequest="{OnIdiom Tablet=410, Default=380}"
     ShouldAutoPlay="True"
     Source="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
     MetaDataArtworkUrl="https://michellevella.com/cdn/shop/products/BeatlesLonelyHearts4UP_1728x.jpg?v=1660582971"
     MetaDataTitle="Big Buck Bunny"
     MetaDataArtist="Blender Foundation"
     SourceType="Audio"
   MediaEnded="OnMediaEnded"
     MediaFailed="OnMediaFailed"
     MediaOpened="OnMediaOpened"
     PositionChanged="OnPositionChanged"
     StateChanged="OnStateChanged"
     SeekCompleted="OnSeekCompleted"/>

Example in code behind:

async void ChangeSourceClicked(Object sender, EventArgs e)
{
	var result = await DisplayActionSheet("Choose a source", "Cancel", null,
		loadOnlineMp4, loadHls, loadLocalResource, resetSource);

	switch (result)
	{
		case loadOnlineMp4:
			MediaElement.MetaDataTitle = "Big Buck Bunny";
			MediaElement.MetaDataArtworkUrl = "https://michellevella.com/cdn/shop/products/BeatlesLonelyHearts4UP_1728x.jpg?v=1660582971";
			MediaElement.MetaDataArtist = "Big Buck Bunny Album";
			MediaElement.SourceType = Primitives.MediaElementSourceType.Audio;
			MediaElement.Source =
				MediaSource.FromUri(
					"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4");
			return;

		case loadHls:
			MediaElement.MetaDataArtist = "HLS Album";
			MediaElement.MetaDataArtworkUrl = "https://michellevella.com/cdn/shop/products/BeatlesLonelyHearts4UP_1728x.jpg?v=1660582971";
			MediaElement.MetaDataTitle = "HLS Title";
			MediaElement.SourceType = Primitives.MediaElementSourceType.Audio;
			MediaElement.Source
				= MediaSource.FromUri(
					"https://mtoczko.github.io/hls-test-streams/test-gap/playlist.m3u8");
			return;

		case resetSource:
			MediaElement.MetaDataArtworkUrl = string.Empty;
			MediaElement.MetaDataTitle = string.Empty;
			MediaElement.MetaDataArtist = string.Empty;
			MediaElement.SourceType = Primitives.MediaElementSourceType.Unknown;
			MediaElement.Source = null;
			return;

		case loadLocalResource:
			MediaElement.MetaDataArtworkUrl = "https://michellevella.com/cdn/shop/products/BeatlesLonelyHearts4UP_1728x.jpg?v=1660582971";
			MediaElement.MetaDataTitle = "Local Resource Title";
			MediaElement.MetaDataArtist = "Local Resource Album";
			MediaElement.SourceType = Primitives.MediaElementSourceType.Audio;

			if (DeviceInfo.Platform == DevicePlatform.MacCatalyst
				|| DeviceInfo.Platform == DevicePlatform.iOS)
			{
				MediaElement.Source = MediaSource.FromResource("AppleVideo.mp4");
			}
			else if (DeviceInfo.Platform == DevicePlatform.Android)
			{
				MediaElement.Source = MediaSource.FromResource("AndroidVideo.mp4");
			}
			else if (DeviceInfo.Platform == DevicePlatform.WinUI)
			{
				MediaElement.Source = MediaSource.FromResource("WindowsVideo.mp4");
			}
			return;
	}
}

Sample API

/// <summary>
/// Gets or sets the Title of the media.
/// This is a bindable property.
/// </summary>
public string MetaDataTitle
{
	get => (string)GetValue(MetaDataTitleProperty);
	set => SetValue(MetaDataTitleProperty, value);
}

/// <summary>
/// Gets or sets the Artist of the media.
/// This is a bindable property.
/// </summary>
public string MetaDataArtist
{
	get => (string)GetValue(MetaDataArtistProperty);
	set => SetValue(MetaDataArtistProperty, value);
}

/// <summary>
/// Gets or sets the Artwork Image Url of the media.
/// This is a bindable property.
/// </summary>
public string MetaDataArtworkUrl
{
	get => (string)GetValue(MetaDataArtworkUrlProperty);
	set => SetValue(MetaDataArtworkUrlProperty, value);
}

/// <summary>
/// Gets or sets the source type of the media.
/// This is a bindable property.
/// </summary>
public MediaElementSourceType SourceType
{
	get => (MediaElementSourceType)GetValue(MediaElementSourceTypeProperty);
	set => SetValue(MediaElementSourceTypeProperty, value);
}

Android and Windows

iOS

Mac Catalyst

…perties to the `MediaElement` class and the creation of a new `MediaElementSourceType.cs` file. The metadata properties allow the media player to display more information about the media being played, such as title, album, artist, genre, artwork, and source type. The `MediaElementSourceType.cs` file introduces a new `enum` type that represents different types of media sources.

1. Added new properties to the `MediaElement` class to support metadata such as title, album, artist, genre, artwork, and source type. This allows the media player to display more information about the media being played.
2. Updated the `MediaElementPage.xaml` and `MediaElementPage.xaml.cs` files to use the new metadata properties in the `MediaElement` class.
3. Added new code in the `MediaManager.macios.cs` and `MediaManager.windows.cs` files to handle the metadata properties when playing media.
4. Added new code in the `Info.plist` file to allow the app to play audio in the background on iOS.
5. Added a new `MetaDataExtensions` class to handle setting the metadata for the system media controls on iOS and Windows.
6. Updated the `IMediaElement` interface to include the new metadata properties.
7. Updated the `MediaElement.shared.cs` file to include the new bindable properties for the metadata.
8. Updated the `MediaManager` class to handle the metadata when the media source is changed.
9. Updated the `MediaManager` class to update the metadata when the media state changes.
10. Updated the `MediaManager` class to clear the metadata when the media source is reset.
11. Created a new file named `MediaElementSourceType.cs` in the `CommunityToolkit.Maui.Primitives` namespace. This file defines a new `enum` type named `MediaElementSourceType` that represents different types of media sources, including `Video`, `Audio`, and `Unknown`.
…, the addition of new permissions and services, changes to the launch mode, and the addition of new vector graphics and shapes. The `CommunityToolkit.Maui.Sample.csproj` and `CommunityToolkit.Maui.MediaElement.csproj` files were updated to include new `PropertyGroup` and `PackageReference`. The `AndroidManifest.xml` file was updated to include new permissions and a new service was added to the application. The `MainActivity.cs` file was updated to change the `LaunchMode` from `SingleTask` to `SingleInstance`. Several new XML files were added, which define various vector graphics and shapes used in the application's UI.

Here is a list of the changes:

1. `CommunityToolkit.Maui.Sample.csproj` file was updated to include a new `PropertyGroup` and a new `PackageReference` was added.
2. `AndroidManifest.xml` file was updated to include new permissions and a new service was added to the application.
3. `MainActivity.cs` file was updated to change the `LaunchMode` from `SingleTask` to `SingleInstance`.
4. `AppBuilderExtensions.shared.cs` file was updated to include new using directives and a new service was added to the `MauiAppBuilder`.
5. `CommunityToolkit.Maui.MediaElement.csproj` file was updated to include a new `PackageReference`.
6. `MediaManager.android.cs` file was updated to include new using directives, new properties, and new methods. The `Dispose` method was also overridden to include additional cleanup logic.
7. New permissions were added in the `AndroidPermissions.android.cs` file.
8. Several new XML files were added, which define various vector graphics and shapes used in the application's UI.
9. The `ic_forward_30_black_32dp.xml` file was updated to include a new path data for the vector graphic.
10. Added Apache License 2.0 to multiple XML files.
11. Added new vector XML files for various icons.
12. Added Apache License, Version 2.0 to the source files.
13. Added vector graphics for a thumb up icon in `ic_thumb_up_black_32dp.xml`.
14. Added a gradient background shape in `media_controls_mask_background.xml`.
15. Added a solid color shape with stroke, corners, and padding in `media_details_mask_background.xml`.
16. Added a selector for tab indicator in `tab_indicator.xml`.
17. Added a shape for test result divider in `test_result_divider.xml`.
18. Added a gradient background shape with padding and size in `tv_bg.xml`.
19. Added color resources in `colors.xml`.
20. Added dimension resources in `dimens.xml`.
21. Added string resources in `strings.xml`.
22. Added styles in `styles.xml`.
23. Changes in `testing_strings.xml` involve the addition of a large number of string resources.
24. Changes in `MediaControlsService.android.cs` involve the creation of a new `MediaControlsService` class.
…trolsService` and `MediaManager` classes in the `MediaControlsService.android.cs` and `MediaManager.android.cs` files respectively. Both classes have been updated to use new methods from the `MetaDataExtensions` class for setting notifications and metadata, instead of using the `MediaMetadataCompat.Builder` and `AndroidX.Core.App.NotificationCompat.Builder` classes directly. Additionally, the `startForegroundService` method in `MediaControlsService.android.cs` was renamed to `startForegroundServiceAsync`, made asynchronous, and had its parameters updated. A new file, `MetaDataExtensions.android.cs`, was also added, containing extension methods for setting notifications and metadata.

1. Unused `using` directives were removed from `MediaControlsService.android.cs` and `MediaManager.android.cs`, and new ones were added to `MetaDataExtensions.android.cs`.
2. The `startForegroundService` method in `MediaControlsService.android.cs` was renamed to `startForegroundServiceAsync`, made asynchronous, and had its parameters changed.
3. The `MediaControlsService` constructor was modified to initialize `bitmap` and `stateBuilder` to `null`.
4. The `MediaControlsService` and `MediaManager` classes were updated to use the new `MetaDataExtensions` methods for setting notifications and metadata.
5. The `MediaControlsService` and `MediaManager` classes no longer use the `MediaMetadataCompat.Builder` and `AndroidX.Core.App.NotificationCompat.Builder` classes directly.
6. The `OnMediaSessionStop` method was removed from `MediaManager.android.cs`.
7. The `CheckAndRequestForeGroundPermission` method in `MediaManager.android.cs` was made static and no longer returns a status.
8. The `StartService` and `StopService` methods in `MediaManager.android.cs` were made protected, and `StopService` was made static.
9. A new file, `MetaDataExtensions.android.cs`, was added, containing extension methods for setting notifications and metadata, as well as a method for getting a `Bitmap` from a URL.
…`duration` to several methods in different files. This parameter is used to set the duration of the media in various contexts. Additionally, the `SetMetadata` method now includes a `position` parameter. The keys used in the `SetMetadata` method have also been updated.

1. The `SetNotifications` method in `MetaDataExtensions.android.cs` now includes a `duration` parameter, which is used to set the duration of the media in the notification. (See `MetaDataExtensions.android.cs`)

2. The `SetMetadata` method in `MetaDataExtensions.android.cs` now includes `duration` and `position` parameters, which are used to set the duration and position of the media in the metadata. (See `MetaDataExtensions.android.cs`)

3. The `startForegroundServiceAsync` method in `MediaControlsService.android.cs` now includes a `duration` parameter, which is used when calling the `SetNotifications` and `SetMetadata` methods. (See `MediaControlsService.android.cs`)

4. The `OnStartCommand` method in `MediaControlsService.android.cs` has been updated to retrieve the `duration` from the intent extras and pass it to the `startForegroundServiceAsync` method. (See `MediaControlsService.android.cs`)

5. The `StartService` method in `MediaManager.android.cs` now puts the `duration` into the intent extras, and the `position` and `currentTime` values are now being cast to `long` instead of `int`. (See `MediaManager.android.cs`)

6. The keys used in the `SetMetadata` method in `MetaDataExtensions.android.cs` have been updated to `MediaMetadata.MetadataKeyAlbumArtist`, `MediaMetadata.MetadataKeyArtist`, `MediaMetadata.MetadataKeyTitle`, and `MediaMetadata.MetadataKeyAlbumArt`. (See `MetaDataExtensions.android.cs`)
…ndroid.cs` and `MediaManager.android.cs` files. In `MediaControlsService.android.cs`, new functionality related to audio focus and volume control was added, and the `startForegroundServiceAsync` method was significantly refactored to include these new features. The `createNotificationChannel` method was also renamed to `CreateNotificationChannel` to adhere to C# naming conventions.

Changes:

1. In `MetaDataExtensions.macios.cs`, a redundant return statement was removed from the `SetMetaData` method.
2. In `MetaDataExtensions.shared.cs`, the summary comments for the `MetaDataExtensions` class and its constructor were updated to provide a more detailed explanation of the class's purpose.
3. In `MediaControlsService.android.cs`, two new `using` directives were added for `Android.Media` and `Stream = Android.Media.Stream`. The `MediaControlsService` class was updated to include new functionality related to audio focus and volume control. The `startForegroundServiceAsync` method was refactored to include these new features, and the `createNotificationChannel` method was renamed to `CreateNotificationChannel`.
@ne0rrmatrix ne0rrmatrix changed the title Meta data Lock Screen Controls and Metadata Support for Windows, Android, IOS and Mac Catalyst Mar 31, 2024
…l actions, the replacement of `Android.Support.V4.Media.Session.PlaybackStateCompat.Builder` with `PlaybackStateCompat.Builder`, and the inclusion of the `System.Net` namespace in the `MediaManager.android.cs` file.

1. The `AndroidManifest.xml` file has been updated to include a new permission for `MEDIA_CONTENT_CONTROL`. This change allows the application to control media content within its environment.

2. The `SetNotifications` method in `MetaDataExtensions.android.cs` has been updated to include additional media control actions such as "Previous", "Pause", "Play", "Next", "FastForward", and "Rewind". The notification icon has also been changed from `exo_ic_audiotrack` to `exo_styled_controls_audiotrack`.

3. The `MediaControlsService.android.cs` file has been updated to replace `Android.Support.V4.Media.Session.PlaybackStateCompat.Builder` with `PlaybackStateCompat.Builder`. This change simplifies the code and improves compatibility. Additionally, a new task has been added to asynchronously fetch the bitmap from a URL.

4. The `startForegroundServiceAsync` method in `MediaControlsService.android.cs` has been updated to change the actions set in the `PlaybackStateCompat.Builder`. This change allows the application to control the playback state.

5. The `MediaManager.android.cs` file has been updated to include the `System.Net` namespace. This change allows the application to use classes and methods for sending data to and receiving data from a resource identified by a URI. The `MediaManager` class has been updated to set the enabled playback actions in the `MediaSessionConnector` and to add the `FlagHandlesQueueCommands` flag to the `MediaSessionCompat`.

6. The `PlayerView` in `MediaManager.android.cs` has been updated to disable the controller auto show and use controller options. This change gives the application more control over the player view.
Update now playing info with avplayer to true if using Mac. False for IOS.
… handle UpdatesNowPlayingInfoCenter based on platform in MediaManager.macios.cs
…nsions` class from several files and the addition of new namespaces and objects in `MediaControlsService.android.cs` and `MediaManager.android.cs`. The `startForegroundServiceAsync` and `StartService` methods in these files were also modified, and a `Dispose` method was added to `MediaControlsService.android.cs`.

1. The `MetaDataExtensions` class was removed from `MetaDataExtensions.android.cs` and `MetaDataExtensions.shared.cs`, and its methods were moved to other classes. It was also changed from a partial class to a regular class in `MetaDataExtensions.macios.cs` and `MetaDataExtensions.windows.cs`.
2. The `MediaControlsService.android.cs` file saw the addition of `System.Runtime.InteropServices` and `Microsoft.Win32.SafeHandles` namespaces, and a rearrangement of the order of namespaces. A `SafeHandle` object and an `AudioManager` object were also added to this file.
3. The `startForegroundServiceAsync` method in `MediaControlsService.android.cs` was modified to remove parameters and change the implementation. A `Dispose` method was also added to this file to dispose of resources.
4. The `MediaManager.android.cs` file saw the addition of `Android.Content.Res`, `Android.Graphics`, `Android.Support.V4.Media`, and `Resource` namespaces, and a rearrangement of the order of namespaces. The `GetBitmapFromUrl` method was moved from `MetaDataExtensions.android.cs` to this file.
5. The `StartService` method in `MediaManager.android.cs` was modified to remove parameters and change the implementation. The `assetFilePath` in the `OnVolumeChanged` method was changed to use `System.IO.Path.PathSeparator` instead of `Path.PathSeparator`, and the `StartService` method call was changed to be asynchronous.
…ndroid.cs` and `MediaManager.android.cs` files. In `MediaControlsService.android.cs`, the `MediaControlsService` class was updated to be enabled by default and the `startForegroundServiceAsync` method was modified to accept an `Intent` object instead of a `MediaSessionCompat.Token` object. The `OnStartCommand` method was updated to handle different media actions and the `Dispose` method was updated to abandon audio focus. In `MediaManager.android.cs`, a static `MediaControllerCompat` object was added to the `MediaManager` class and the constructor was updated to set the media session to active. The `CheckAndRequestForeGroundPermission` method was updated to display an alert if the notification permission is denied and the `StartService` method was updated to put extra information into the intent.

Changes:

1. In `AndroidManifest.xml`, an intent filter was added to the `CommunityToolkit.Maui.Services` service to handle `android.intent.action.MEDIA_BUTTON` actions.
2. In `MediaControlsService.android.cs`, the `MediaControlsService` class was updated to be enabled by default.
3. The `startForegroundServiceAsync` method in `MediaControlsService.android.cs` was updated to accept an `Intent` object instead of a `MediaSessionCompat.Token` object.
4. The `OnStartCommand` method in `MediaControlsService.android.cs` was updated to handle different media actions.
5. The `Dispose` method in `MediaControlsService.android.cs` was updated to abandon audio focus and set the audio manager's parameters to "Ducking=false".
6. In `MediaManager.android.cs`, a static `MediaControllerCompat` object was added to the `MediaManager` class.
7. The `MediaManager` constructor in `MediaManager.android.cs` was updated to set the media session to active.
8. The `CheckAndRequestForeGroundPermission` method in `MediaManager.android.cs` was updated to display an alert if the notification permission is denied.
9. The `StartService` method in `MediaManager.android.cs` was updated to put extra information into the intent.
…lve the addition of new fields and methods, and the refactoring of existing methods to improve readability and maintainability. The new fields `notification` and `pendingIntentFlags` have been added, and the `startForegroundServiceAsync` method has been broken down into three new methods: `OnSetupAudioServices`, `OnSetContent`, and `OnSetIntents`. The `OnStartCommand` method has been refactored to use a switch statement for better readability. A new static method `CreateNotificationChannel` has also been added.

List of changes:

1. Addition of a new `NotificationCompat.Builder?` field named `notification` and a `PendingIntentFlags` field named `pendingIntentFlags` to the `MediaControlsService` class.
2. Refactoring of the `startForegroundServiceAsync` method to improve readability. The setup of audio services, setting of content, and setting of intents have been moved to their own methods: `OnSetupAudioServices`, `OnSetContent`, and `OnSetIntents`.
3. Refactoring of the `OnStartCommand` method to use a switch statement instead of multiple if-else statements for better readability.
4. Addition of a new static method `CreateNotificationChannel` to create a notification channel.
5. The `Dispose` method remains unchanged.

These changes adhere to the Single Responsibility Principle of SOLID design principles, making the code easier to understand and maintain.
@ne0rrmatrix ne0rrmatrix marked this pull request as ready for review April 4, 2024 06:24
…properties related to media metadata in a media player application. The properties `MetadataTitle`, `MetadataAlbum`, `MetadataArtist`, `MetadataGenre`, and `MetadataArtwork` have been replaced with `ContentTitle`, `ContentText`, and `LargeImageUrl` respectively. These changes have been implemented across multiple files, including the interface `IMediaElement` and the media element's metadata properties in `MediaElementPage.xaml` and `MediaElementPage.xaml.cs`.

List of changes:

1. Renamed and redefined media metadata properties from `MetadataTitle`, `MetadataAlbum`, `MetadataArtist`, `MetadataGenre`, and `MetadataArtwork` to `ContentTitle`, `ContentText`, and `LargeImageUrl` across multiple files.
2. Updated the media element's metadata properties in `MediaElementPage.xaml` to use the new properties.
3. Set the media element's metadata properties to new values based on the source of the media in `MediaElementPage.xaml.cs`.
4. Updated the media metadata to use the new properties in `MetaDataExtensions.macios.cs` and `MetaDataExtensions.windows.cs`.
5. Updated the interface `IMediaElement` to include the new properties in `IMediaElement.cs`.
6. Updated the bindable properties and their getters and setters to reflect the new properties in `MediaElement.shared.cs`.
7. Updated the media metadata to use the new properties when starting the service in `MediaManager.android.cs`.
…properties related to media metadata in a media player application. The properties `ContentTitle`, `ContentText`, and `LargeImageUrl` have been renamed to `MetaDataTitle`, `MetaDataArtist`, and `MetaDataArtworkUrl` respectively. These changes have been implemented across multiple files.

1. In `MediaElementPage.xaml`, the properties `LargeImageUrl`, `ContentTitle`, and `ContentText` have been replaced with `MetaDataArtworkUrl`, `MetaDataTitle`, and `MetaDataArtist` respectively. (See `MediaElementPage.xaml` changes)
2. The `ChangeSourceClicked` method in `MediaElementPage.xaml.cs` has been updated to use the new property names for different cases including `loadOnlineMp4`, `loadHls`, `resetSource`, and `loadLocalResource`. (See `MediaElementPage.xaml.cs` changes)
3. The `SetMetaData` method in `MetaDataExtensions.macios.cs` and `MetaDataExtensions.windows.cs` has been updated to use the new property names. (See `MetaDataExtensions.macios.cs` and `MetaDataExtensions.windows.cs` changes)
4. The interface `IMediaElement` in `IMediaElement.cs` has been updated to reflect the new property names. (See `IMediaElement.cs` changes)
5. The bindable properties and their getters and setters in `MediaElement.shared.cs` have been updated to reflect the new property names. (See `MediaElement.shared.cs` changes)
6. The `StartService` method in `MediaManager.android.cs` has been updated to use the new property names. (See `MediaManager.android.cs` changes)
… `startForegroundServiceAsync` method in the `MediaControlsService.android.cs` file. This line was setting the session activity for the media session, but it was deemed unnecessary as the same line of code is present later in the method.

Changes:
- Removed the line `mediaSession?.SetSessionActivity(pendingIntent);` from the `startForegroundServiceAsync` method in the `MediaControlsService.android.cs` file. This change was made because the same line of code is present later in the method, making this line redundant. (Reference: `MediaControlsService.android.cs`)
…od in `MediaControlsService.android.cs`. This change is aimed at enhancing resource management and preventing potential memory leaks.

Changes:
- The `Dispose` method in `MediaControlsService.android.cs` has been updated. A new line of code has been added to release the `mediaSession` before it is disposed of. This is a crucial change as it ensures that any resources held by the `mediaSession` are properly released, thereby preventing potential memory leaks or other resource management issues.

Reference to the code changes:
- Modification of `Dispose` method in `MediaControlsService.android.cs` to include a line of code that releases the `mediaSession` before disposing it.
…he `MediaControlsService` and `MediaManager` classes, the removal of content from several XML files, and the addition of new classes to handle updates and notifications.

1. The `MediaControlsService` class in `MediaControlsService.android.cs` has been extensively refactored. New constants for media actions have been added, and methods have been updated to handle media actions and notification events differently. A new `ReceiveUpdates` class has been added to listen for updates from the `MediaManager`. The `Dispose` method has been updated to unregister and dispose the `ReceiveUpdates` object.

2. The `MediaManager` class in `MediaManager.android.cs` has been updated to create and register a new `UIUpdateReceiver` object. The `StartService` and `OnPlaybackStateChanged` methods have been updated to send broadcasts with the current action and start the service when the player is ready. The `Dispose` method has been updated to unregister and dispose the `UIUpdateReceiver` object. A new `UIUpdateReceiver` class has been added to listen for updates from the `MediaControlsService`.

3. The entire content of several XML files, including `strings.xml`, `styles.xml`, `testing_strings.xml`, and various icon and background files, have been removed. This suggests a significant refactoring or restructuring of the project, or the removal of unused or deprecated resources.

References to the code changes can be found in the `MediaControlsService.android.cs`, `MediaManager.android.cs`, `strings.xml`, `styles.xml`, `testing_strings.xml`, and various other XML files.
…d namespaces in `MediaControlsService.android.cs`, the addition of new methods for handling notification events, setting up audio services, broadcasting updates, and disposing resources. Additionally, the accessibility of certain methods in `MediaControlsService.android.cs` and `MediaManager.android.cs` has been limited by changing them from public or protected to private. Error handling has been improved with the addition of null checks and the replacement of a null check with `ArgumentNullException.ThrowIfNull(intent)`.

Here are the changes in detail:

1. Removed the usage of `Android.Views` and `Com.Google.Android.Exoplayer2` namespaces in `MediaControlsService.android.cs` as they are no longer needed.
2. The `startForegroundServiceAsync` method was changed from public to private in `MediaControlsService.android.cs` to limit its accessibility.
3. The null check for `intent` in `OnStartCommand` method was replaced with `ArgumentNullException.ThrowIfNull(intent)` in `MediaControlsService.android.cs` for better error handling.
4. A new method `HandleNotificationEvent` was added in `MediaControlsService.android.cs` to handle notification events.
5. A new method `OnSetupAudioServices` was added in `MediaControlsService.android.cs` to setup audio services.
6. A new method `BroadcastUpdate` was added in `MediaControlsService.android.cs` to broadcast updates.
7. A `Dispose` method was added in `MediaControlsService.android.cs` to properly dispose resources.
8. A `PropertyChanged` event was added in `MediaControlsService.android.cs` to notify when a property has changed.
9. The `GetAction` method in `MediaControlsService.android.cs` was changed to return `Action` directly instead of `Action ?? string.Empty`.
10. Null checks for `mediaSessionConnector`, `Platform.CurrentActivity`, and `mediaSession.SessionToken` were added in `MediaManager.android.cs` to prevent null reference exceptions.
11. The `CheckAndRequestForeGroundPermission` method was changed from protected to private in `MediaManager.android.cs` to limit its accessibility.
12. The `StopService` method was changed from protected to private in `MediaManager.android.cs` to limit its accessibility.
…gleInstance` to `SingleTask`. Lastly, the `OnReceive` method in `MediaManager.android.cs` was refactored to use a switch statement and the `player` field was assigned directly in the constructor.

1. `MainActivity.cs`: The `LaunchMode` attribute of the `Activity` annotation was changed from `SingleInstance` to `SingleTask`.
2. `MediaManager.android.cs`: The `OnReceive` method was refactored to use a switch statement instead of multiple if-else statements. The `player` field was assigned directly in the `UIUpdateReceiver` constructor.

The changes in `MediaElementPage.xaml`, `CommunityToolkit.Maui.MediaElement.csproj`, and `MediaManager.windows.cs` were minor and did not affect the functionality of the code.
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.

@ne0rrmatrix, I took the liberty of taking this and moving forward, so I refactor all the code related to the async tasks, that way I hope to unblock you and this PR. Let me know if you've any question on what I did, I'll be more than happy to provide further context.

Not related to this PR, but while working on it I just saw that ExoPlayer is obsolete, they moved it to AndroidX lib, more info here, should we look and migrate to this lib?

@ne0rrmatrix
Copy link
Contributor Author

@ne0rrmatrix, I took the liberty of taking this and moving forward, so I refactor all the code related to the async tasks, that way I hope to unblock you and this PR. Let me know if you've any question on what I did, I'll be more than happy to provide further context.

Not related to this PR, but while working on it I just saw that ExoPlayer is obsolete, they moved it to AndroidX lib, more info here, should we look and migrate to this lib?

@pictos thank you for fixing all the code. I appreciate it. I will look and try to remember the pattern for async await that you have implemented. You did great! BTW we are currently blocked for updating to AndroidX as the bindings are not out yet. But there is an open PR with regards to this that should start the process of migration once the bindings are available. Or at least I saw a team member had started a discussion about it with regards to media element.

pictos and others added 4 commits April 21, 2024 23:01
…pFromUrl` method in `MediaManager.android.cs` and `MediaControlsService.android.cs`. The `Resources` parameter was removed and the method of creating a default bitmap was altered. This change simplifies the method and removes its dependency on `Resources` and `Resource.Drawable.exo_ic_default_album_image`. Additionally, the `using Resource = Microsoft.Maui.Resource;` statement was removed from `MediaManager.android.cs`, likely due to the `Resource` no longer being needed after the refactoring. Lastly, the calls to `GetBitmapFromUrl` in `OnSetContent` and `StartService` methods were updated to match the new signature of `GetBitmapFromUrl`, with the `Resources` argument being removed.

Changes:
1. Refactoring of `GetBitmapFromUrl` method in `MediaManager.android.cs` and `MediaControlsService.android.cs` to remove `Resources` parameter and change the way a default bitmap is created.
2. Removal of `using Resource = Microsoft.Maui.Resource;` statement from `MediaManager.android.cs`.
3. Update of calls to `GetBitmapFromUrl` in `OnSetContent` and `StartService` methods in `MediaControlsService.android.cs` and `MediaManager.android.cs` to match the new signature of `GetBitmapFromUrl`, removing the `Resources` argument.
…etaDataArtworkUrl` property of the `MediaElement` object and attribute in the `MediaElementPage.xaml.cs` and `MediaElementPage.xaml` files respectively. The previous URL has been replaced with a new one.

Changes:
1. In the `MediaElementPage.xaml.cs` file, the URL for the `MetaDataArtworkUrl` property of the `MediaElement` object has been updated in three places within the `ChangeSourceClicked` method. The new URL is "https://lh3.googleusercontent.com/pw/AP1GczNRrebWCJvfdIau1EbsyyYiwAfwHS0JXjbioXvHqEwYIIdCzuLodQCZmA57GADIo5iB3yMMx3t_vsefbfoHwSg0jfUjIXaI83xpiih6d-oT7qD_slR0VgNtfAwJhDBU09kS5V2T5ZML-WWZn8IrjD4J-g=w1792-h1024-s-no-gm".
2. The `MetaDataArtworkUrl` attribute of the `toolkit:MediaElement` element in the `MediaElementPage.xaml` file has been updated with the same new URL.
@ne0rrmatrix ne0rrmatrix requested a review from pictos April 26, 2024 03:03
pictos
pictos previously approved these changes Apr 29, 2024
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.

LGTM! Awaiting for other reviewers

@ne0rrmatrix
Copy link
Contributor Author

Before we pull the trigger does anyone want to update the doc's with how to add metadata? I can do that or does someone who is good with Markdown want to do it?

@ne0rrmatrix
Copy link
Contributor Author

At a minimum the docs need to be updated for android manifest changes. plist changes for mac and iOS needs to be done too. Updating the docs for how to use and set the metadata would be useful too.

@ne0rrmatrix
Copy link
Contributor Author

Updated docs. Link to PR: MicrosoftDocs/CommunityToolkit#414

…roidManifest.xml and MediaControlsService.android.cs files. The service name has been changed from "CommunityToolkit.Maui.Services" to "CommunityToolkit.Maui.Media.Services", indicating a more specific focus on media functionality within the CommunityToolkit.Maui application.

List of Changes:
1. The service name in the AndroidManifest.xml file has been changed from "CommunityToolkit.Maui.Services" to "CommunityToolkit.Maui.Media.Services". This change reflects the service's new focus on media functionality within the CommunityToolkit.Maui application. (Reference: AndroidManifest.xml)
2. Similarly, the service name in the MediaControlsService.android.cs file has been updated from "CommunityToolkit.Maui.Services" to "CommunityToolkit.Maui.Media.Services", further emphasizing the service's media-specific role. (Reference: MediaControlsService.android.cs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants