Skip to content

Commit 14eab7b

Browse files
committed
Add support to make AndroidForegroundService optional to MediaElement
Updated MediaElement to include AndroidForegroundServiceEnabled property for better control over media playback on Android. Adjusted related methods and classes to handle this new property, ensuring the foreground service is utilized appropriately during media playback. Removed unnecessary properties and streamlined the popup media element configuration.
1 parent 12adc49 commit 14eab7b

File tree

6 files changed

+23
-5
lines changed

6 files changed

+23
-5
lines changed

samples/CommunityToolkit.Maui.Sample/Pages/Views/MediaElement/MediaElementPage.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<toolkit:MediaElement
3131
x:Name="MediaElement"
3232
ShouldAutoPlay="True"
33+
AndroidForegroundServiceEnabled="True"
3334
Source="{x:Static constants:StreamingVideoUrls.BuckBunny}"
3435
MetadataArtworkUrl="https://lh3.googleusercontent.com/pw/AP1GczNRrebWCJvfdIau1EbsyyYiwAfwHS0JXjbioXvHqEwYIIdCzuLodQCZmA57GADIo5iB3yMMx3t_vsefbfoHwSg0jfUjIXaI83xpiih6d-oT7qD_slR0VgNtfAwJhDBU09kS5V2T5ZML-WWZn8IrjD4J-g=w1792-h1024-s-no-gm"
3536
MetadataTitle="Big Buck Bunny"

samples/CommunityToolkit.Maui.Sample/Pages/Views/MediaElement/MediaElementPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ void DisplayPopup(object sender, EventArgs e)
250250
{
251251
AndroidViewType = AndroidViewType.SurfaceView,
252252
Source = MediaSource.FromResource("AppleVideo.mp4"),
253-
MetadataArtworkUrl = botImageUrl,
253+
AndroidForegroundServiceEnabled = false,
254254
HeightRequest = 600,
255255
WidthRequest = 600,
256256
ShouldAutoPlay = true,

src/CommunityToolkit.Maui.MediaElement/Handlers/MediaElementHandler.android.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected override MauiMediaElement CreatePlatformView()
2424
VirtualView,
2525
Dispatcher.GetForCurrentThread() ?? throw new InvalidOperationException($"{nameof(IDispatcher)} cannot be null"));
2626

27-
var (_, playerView) = MediaManager.CreatePlatformView(VirtualView.AndroidViewType);
27+
var (_, playerView) = MediaManager.CreatePlatformView(VirtualView.AndroidViewType, VirtualView.AndroidForegroundServiceEnabled);
2828
return new(Context, playerView);
2929
}
3030

src/CommunityToolkit.Maui.MediaElement/MediaElement.shared.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ internal event EventHandler StopRequested
225225
/// </summary>
226226
public AndroidViewType AndroidViewType { get; init; } = MediaElementOptions.DefaultAndroidViewType;
227227

228+
/// <summary>
229+
/// Gets or sets whether the Android Foreground Service is enabled.
230+
/// </summary>
231+
public bool AndroidForegroundServiceEnabled { get; init; } = MediaElementOptions.AndroidForeServiceEnabled;
232+
228233
/// <summary>
229234
/// Gets or sets whether the media should start playing as soon as it's loaded.
230235
/// Default is <see langword="false"/>. This is a bindable property.

src/CommunityToolkit.Maui.MediaElement/MediaElementOptions.shared.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,19 @@ internal MediaElementOptions(in MauiAppBuilder builder) : this()
1717
/// </summary>
1818
internal static AndroidViewType DefaultAndroidViewType { get; private set; } = AndroidViewType.SurfaceView;
1919

20+
/// <summary>
21+
/// Set Android Foreground Service for MediaElement on construction
22+
/// </summary>
23+
internal static bool AndroidForeServiceEnabled { get; private set; } = true;
24+
2025
/// <summary>
2126
/// Set Android View type for MediaElement as SurfaceView or TextureView on construction
2227
/// </summary>
2328
public void SetDefaultAndroidViewType(AndroidViewType androidViewType) => DefaultAndroidViewType = androidViewType;
29+
30+
/// <summary>
31+
/// Set Android Foreground Service for MediaElement on construction
32+
/// </summary>
33+
/// <param name="androidForegroundServiceEnabled"></param>
34+
public void SetDefaultAndroidForegroundService(bool androidForegroundServiceEnabled) => AndroidForeServiceEnabled = androidForegroundServiceEnabled;
2435
}

src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.android.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace CommunityToolkit.Maui.Core.Views;
2020

2121
public partial class MediaManager : Java.Lang.Object, IPlayerListener
2222
{
23+
bool androidForegroundServiceEnabled;
2324
const int bufferState = 2;
2425
const int readyState = 3;
2526
const int endedState = 4;
@@ -129,11 +130,11 @@ or PlaybackState.StateSkippingToQueueItem
129130
/// <returns>The platform native counterpart of <see cref="MediaElement"/>.</returns>
130131
/// <exception cref="NullReferenceException">Thrown when <see cref="Context"/> is <see langword="null"/> or when the platform view could not be created.</exception>
131132
[MemberNotNull(nameof(Player), nameof(PlayerView), nameof(session))]
132-
public (PlatformMediaElement platformView, PlayerView PlayerView) CreatePlatformView(AndroidViewType androidViewType)
133+
public (PlatformMediaElement platformView, PlayerView PlayerView) CreatePlatformView(AndroidViewType androidViewType, bool androidForegroundServiceEnabled)
133134
{
134135
Player = new ExoPlayerBuilder(MauiContext.Context).Build() ?? throw new InvalidOperationException("Player cannot be null");
135136
Player.AddListener(this);
136-
137+
this.androidForegroundServiceEnabled = androidForegroundServiceEnabled;
137138
if (androidViewType is AndroidViewType.SurfaceView)
138139
{
139140
PlayerView = new PlayerView(MauiContext.Context)
@@ -352,7 +353,7 @@ protected virtual async partial ValueTask PlatformUpdateSource()
352353
return;
353354
}
354355

355-
if (connection is null)
356+
if (connection is null && androidForegroundServiceEnabled)
356357
{
357358
StartService();
358359
}

0 commit comments

Comments
 (0)