Skip to content

Commit 886c0b0

Browse files
Update MediaElement documentation to include foreground service (#613)
* Update MediaElement documentation to include foreground service requirements for Android * Fix minor error * Update MediaElement documentation to include background playback permissions and rich media notifications * Clarify documentation for enabling background video playback in MediaElement * Add documentation for enabling foreground service in MediaElement on Android * Update MediaElement documentation to clarify metadata setup for rich media notifications * Clarify instructions for enabling rich media notifications in MediaElement documentation * Update docs/maui/views/MediaElement.md Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com> * Update docs/maui/views/MediaElement.md Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com> * Update docs/maui/views/MediaElement.md Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com> * Update docs/maui/views/MediaElement.md Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com> * Update docs/maui/get-started.md Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com> * Fix duplication * Add important note for MediaElement usage in MauiProgram.cs * Update MediaElement documentation to clarify foreground service requirements for background playback on Android * Update MediaElement documentation to emphasize the importance of calling UseMauiCommunityToolkitMediaElement for background playback and Rich Media Notifications on Android --------- Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com>
1 parent b0fed82 commit 886c0b0

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

docs/maui/get-started.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,15 @@ using CommunityToolkit.Maui;
138138
```
139139

140140
In order to use the `MediaElement` correctly the `UseMauiCommunityToolkitMediaElement` method must be called on the `MauiAppBuilder` class when bootstrapping an application the *MauiProgram.cs* file. The following example shows how to perform this.
141+
You must specify whether to enable the foreground service on Android by setting the `enableForegroundService` parameter to true or false. If you require Rich Media Notifications or background playback on Android, set `enableForegroundService` to `true` when calling `UseMauiCommunityToolkitMediaElement`.
142+
143+
Note: `MediaElement` does not require adding additional `AndroidManifest.xml` permissions for the foreground service. Calling the builder method with `enableForegroundService: true` is sufficient to enable notification and background playback features. When `enableForegroundService` is `true`, the toolkit will automatically add any required Android manifest entries for the foreground service and notifications. If `enableForegroundService` is `false` and you attempt to use background playback features, an exception may be thrown on Android.
141144

142145
```csharp
143146
var builder = MauiApp.CreateBuilder();
144147
builder
145148
.UseMauiApp<App>()
146-
.UseMauiCommunityToolkitMediaElement()
149+
.UseMauiCommunityToolkitMediaElement(enableForegroundService: true);
147150
```
148151

149152
To use the features of the toolkit please refer to the documentation pages for each specific feature.

docs/maui/views/MediaElement.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,18 @@ First the using statement needs to be added to the top of your *MauiProgram.cs*
4848
```csharp
4949
using CommunityToolkit.Maui.MediaElement;
5050
```
51+
52+
> In order to use the `MediaElement` correctly the `UseMauiCommunityToolkitMediaElement` method must be called on the `MauiAppBuilder` class when bootstrapping an application the *MauiProgram.cs* file. The following example shows how to perform this.
5153
52-
In order to use the `MediaElement` correctly the `UseMauiCommunityToolkitMediaElement` method must be called on the `MauiAppBuilder` class when bootstrapping an application the *MauiProgram.cs* file. The following example shows how to perform this.
54+
55+
> [!IMPORTANT]
56+
> If you require Rich Media Notifications or background playback on Android, set `enableForegroundService` to `true` when calling `UseMauiCommunityToolkitMediaElement`. MediaElement does not require adding additional `AndroidManifest.xml` permissions for the foreground service. When `enableForegroundService` is `true`, the toolkit will automatically add any required Android manifest entries for the foreground service and notifications. If you do not need background playback, you can set this to `false`. If you fail to call this method, an exception will be thrown when trying to use the `MediaElement`.
5357
5458
```csharp
5559
var builder = MauiApp.CreateBuilder();
5660
builder
5761
.UseMauiApp<App>()
58-
.UseMauiCommunityToolkitMediaElement()
62+
.UseMauiCommunityToolkitMediaElement(enableForegroundService: true); // Set to `false` if Rich Notifications and background playback is not required
5963
```
6064

6165
For more information on how to do this, please refer to the [Get Started](../get-started.md?tabs=CommunityToolkitMauiMediaElement#adding-the-nuget-packages) page.
@@ -149,6 +153,15 @@ By default, the media that is defined by the `Source` property doesn't immediate
149153

150154
Platform provided media playback controls are enabled by default, and can be disabled by setting the `ShouldShowPlaybackControls` property to `false`.
151155

156+
### Use Rich Media Notifications
157+
A `MediaElement` can show rich media notifications on Android, iOS, Mac Catalyst, and Windows when media is playing in the background. To enable rich media notifications, the following steps are required:
158+
1. Enable background video playback by setting the `enableForegroundService` parameter to `true` when calling the `UseMauiCommunityToolkitMediaElement` method in *MauiProgram.cs*. Refer to the **Initializing the package** section above for more information.
159+
2. Platform specific setup as described in the [Platform specific initialization](#platform-specific-initialization) section.
160+
3. Set the `MetadataTitle`, `MetadataArtist`, and `MetadataArtworkUrl` properties to provide metadata for the media that is playing as described in the [Using Metadata](#using-metadata) section.
161+
162+
### Foreground service on Android
163+
To enable background playback on Android, set the `enableForegroundService` parameter to `true` when calling the `UseMauiCommunityToolkitMediaElement` method in *MauiProgram.cs*. MediaElement does not require adding additional `AndroidManifest.xml` permissions for the foreground service — calling the builder method with `enableForegroundService: true` is sufficient to enable notification and background playback features. When `enableForegroundService` is `true`, the toolkit will automatically add any required Android manifest entries for the foreground service and notifications. If `enableForegroundService` is `false` and you attempt to use background playback features, an exception may be thrown on Android.
164+
152165
### Using Metadata
153166

154167
A `MediaElement` can use metadata for `MediaElement.MetadataTitle`, `MediaElement.MetadataArtist` and `MediaElement.MetadataArtworkUrl`. You can set

0 commit comments

Comments
 (0)