Skip to content

Add Android TextureView #2540

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

Merged
merged 60 commits into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
c104d70
test project added
jonmdev Feb 17, 2025
7723e5c
add media element to test project
jonmdev Feb 17, 2025
92c2dea
working implementation
jonmdev Feb 19, 2025
18135c5
Add Builder Method
jonmdev Feb 20, 2025
e3af126
comments updated
jonmdev Feb 20, 2025
8f3fdf8
Merge branch 'main' into AddAndroidTextureView
ne0rrmatrix Feb 20, 2025
d0ec322
Remove My Temp Test Project (Unneeded)
jonmdev Feb 20, 2025
d6e2806
Merge branch 'AddAndroidTextureView' of https://github.com/jonmdev/Ma…
jonmdev Feb 20, 2025
6e04927
removed unneeded "using"
jonmdev Feb 20, 2025
55c7b80
comment update
jonmdev Feb 20, 2025
34ace4a
Merge branch 'main' into AddAndroidTextureView
ne0rrmatrix Feb 20, 2025
97d33aa
Update Sample App
ne0rrmatrix Feb 20, 2025
fd54261
Update src/CommunityToolkit.Maui.MediaElement/MediaElement.shared.cs
jonmdev Feb 20, 2025
8423996
switch to "is not null" rather than != null
jonmdev Feb 20, 2025
7f298f5
add 2nd builder method and alternative xml method
jonmdev Feb 21, 2025
fd785b2
3 builder options working
jonmdev Feb 21, 2025
7e6add8
remove stopwatch test as done testing
jonmdev Feb 21, 2025
a8fff6d
refactored redundancies in UseMauiCommunityToolkitMediaElement
jonmdev Feb 21, 2025
86fda83
Merge branch 'main' into AddAndroidTextureView
ne0rrmatrix Feb 21, 2025
6fe89a4
made PlayerView inaccessible to users again
jonmdev Feb 22, 2025
975b739
reduce to one version of UseMauiCommunityToolkitMediaElement
jonmdev Feb 22, 2025
43e222e
Merge branch 'main' into AddAndroidTextureView
ne0rrmatrix Mar 15, 2025
a959f9e
Merge branch 'main' into AddAndroidTextureView
ne0rrmatrix Mar 16, 2025
95f6835
Merge branch 'main' into AddAndroidTextureView
ne0rrmatrix Apr 6, 2025
b3192b7
Merge branch 'main' into AddAndroidTextureView
TheCodeTraveler Apr 12, 2025
eed9323
Merge branch 'main' into AddAndroidTextureView
ne0rrmatrix Apr 22, 2025
41a6b36
Removed second TextureView constructor method
jonmdev Apr 23, 2025
543c271
Remove commented code
TheCodeTraveler Apr 23, 2025
ba1b9cc
Move to `Primitives` Folder, Fix Namespaces
TheCodeTraveler Apr 23, 2025
eb163dd
Refactor MediaElement `Options`, Update Formatting
TheCodeTraveler Apr 23, 2025
a10626e
Demonstrate `AndroidViewType.SurfaceView`
TheCodeTraveler Apr 23, 2025
45b7e23
Use existing ItemGroup
TheCodeTraveler Apr 23, 2025
b71ad8b
Remove unused code
TheCodeTraveler Apr 23, 2025
da87b7e
Add `StreamingVideoUrls`
TheCodeTraveler Apr 23, 2025
d24dde7
Remove duplicate string
TheCodeTraveler Apr 23, 2025
efc97ea
Pass in `AndroidViewType`
TheCodeTraveler Apr 23, 2025
bb54f87
Refactor
TheCodeTraveler Apr 23, 2025
92d52b1
Remove unused code
TheCodeTraveler Apr 23, 2025
ec7e4a1
`dotnet format`
TheCodeTraveler Apr 23, 2025
8fc8666
Remove redundant MediaElement constructor
TheCodeTraveler Apr 23, 2025
fa79138
Use `SurfaceType` for existing MediaElement pages
TheCodeTraveler Apr 23, 2025
9183ee2
Remove embedded videos
TheCodeTraveler Apr 23, 2025
0b39d56
Use `.shared.cs`
TheCodeTraveler Apr 23, 2025
2ecd80b
Update MediaManager.android.cs
TheCodeTraveler Apr 23, 2025
e67d650
Undo changes
TheCodeTraveler Apr 23, 2025
de75055
`dotnet format`
TheCodeTraveler Apr 23, 2025
f14a199
Use `private set;`
TheCodeTraveler Apr 23, 2025
cdd5bcc
Use SurfaceView
TheCodeTraveler Apr 23, 2025
a4c825e
Fix typo
ne0rrmatrix Apr 25, 2025
f00c45d
- Fix sample
ne0rrmatrix Apr 25, 2025
8e739c3
Add unit tests for MediaElement default view type
ne0rrmatrix Apr 25, 2025
9052aca
Fix Tizen MediaManager
ne0rrmatrix Apr 25, 2025
5b59f77
Fix tests
ne0rrmatrix Apr 25, 2025
170e221
Add Label to texture view explaining what it can do
ne0rrmatrix Apr 25, 2025
88a01bf
Use transparent background
TheCodeTraveler Apr 27, 2025
cc76635
Update MediaElementTextureViewPage.cs
TheCodeTraveler Apr 27, 2025
de4d968
Revert "Fix tests"
TheCodeTraveler May 4, 2025
3045aec
Merge branch 'main' into AddAndroidTextureView
TheCodeTraveler May 4, 2025
889006e
Remove TextureViewPage
TheCodeTraveler May 4, 2025
5bf7bff
Update Unit Tests
TheCodeTraveler May 4, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public partial class AppShell : Shell
CreateViewModelMapping<ExpanderPage, ExpanderViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<LazyViewPage, LazyViewViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<MapsPinsPage, MapsPinsViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<MediaElementTextureViewPage, MediaElementTextureViewViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<MediaElementPage, MediaElementViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<MediaElementCarouselViewPage, MediaElementCarouselViewViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
CreateViewModelMapping<MediaElementCollectionViewPage, MediaElementCollectionViewViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace CommunityToolkit.Maui.Sample.Constants;

static class StreamingVideoUrls
{
public const string BuckBunny = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";
public const string ElephantsDream = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4";
public const string Sintel = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/Sintel.mp4";
}
11 changes: 8 additions & 3 deletions samples/CommunityToolkit.Maui.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using CommunityToolkit.Maui.ApplicationModel;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Markup;
using CommunityToolkit.Maui.Media;
using CommunityToolkit.Maui.Sample.Models;
Expand All @@ -26,6 +27,7 @@
using CommunityToolkit.Maui.Sample.ViewModels.Views;
using CommunityToolkit.Maui.Sample.Views.Popups;
using CommunityToolkit.Maui.Storage;
using CommunityToolkit.Maui.Views;
using Microsoft.Extensions.Http.Resilience;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.LifecycleEvents;
Expand Down Expand Up @@ -66,9 +68,11 @@ public static MauiApp CreateMauiApp()
#endif
.UseMauiCommunityToolkitMarkup()
.UseMauiCommunityToolkitCamera()
.UseMauiCommunityToolkitMediaElement()

.ConfigureMauiHandlers(handlers =>
.UseMauiCommunityToolkitMediaElement(static options =>
{
options.SetDefaultAndroidViewType(AndroidViewType.TextureView);
})
.ConfigureMauiHandlers(static handlers =>
{
#if IOS || MACCATALYST
handlers.AddHandler<CollectionView, Microsoft.Maui.Controls.Handlers.Items2.CollectionViewHandler2>();
Expand Down Expand Up @@ -250,6 +254,7 @@ static void RegisterViewsAndViewModels(in IServiceCollection services)
services.AddTransientWithShellRoute<ExpanderPage, ExpanderViewModel>();
services.AddTransientWithShellRoute<LazyViewPage, LazyViewViewModel>();
services.AddTransientWithShellRoute<MapsPinsPage, MapsPinsViewModel>();
services.AddTransientWithShellRoute<MediaElementTextureViewPage, MediaElementTextureViewViewModel>();
services.AddTransientWithShellRoute<MediaElementPage, MediaElementViewModel>();
services.AddTransientWithShellRoute<MediaElementCarouselViewPage, MediaElementCarouselViewViewModel>();
services.AddTransientWithShellRoute<MediaElementCollectionViewPage, MediaElementCollectionViewViewModel>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
</Border.StrokeShape>
<VerticalStackLayout Spacing="6" HeightRequest="250">
<toolkit:MediaElement
AndroidViewType="SurfaceView"
HeightRequest="200"
x:Name="MediaElement"
ShouldAutoPlay="True"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
</Border.StrokeShape>
<VerticalStackLayout Spacing="6" HeightRequest="250">
<toolkit:MediaElement
AndroidViewType="SurfaceView"
HeightRequest="200"
x:Name="MediaElement"
ShouldAutoPlay="True"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Sample.ViewModels.Views;
#if WINDOWS || MACCATALYST
using CommunityToolkit.Maui.Sample.Constants;
using CommunityToolkit.Maui.Views;
#else
using CommunityToolkit.Maui.Markup;
Expand All @@ -9,9 +11,7 @@ namespace CommunityToolkit.Maui.Sample.Pages.Views;

public partial class MediaElementMultipleWindowsPage : BasePage<MediaElementMultipleWindowsViewModel>
{
const string buckBunnyMp4Url = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";
#if WINDOWS || MACCATALYST
const string elephantsDreamMp4Url = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4";
readonly Window secondWindow;
#endif

Expand All @@ -22,14 +22,16 @@ public MediaElementMultipleWindowsPage(MediaElementMultipleWindowsViewModel view
{
Content = new MediaElement
{
Source = elephantsDreamMp4Url,
AndroidViewType= AndroidViewType.SurfaceView,
Source = StreamingVideoUrls.ElephantsDream,
ShouldAutoPlay = true
}
});

Content = new MediaElement
{
Source = buckBunnyMp4Url,
AndroidViewType= AndroidViewType.SurfaceView,
Source = StreamingVideoUrls.BuckBunny,
ShouldAutoPlay = true
};
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:CommunityToolkit.Maui.Sample.Pages"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:constants="clr-namespace:CommunityToolkit.Maui.Sample.Constants"
xmlns:viewModels="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Views"
xmlns:converters="clr-namespace:CommunityToolkit.Maui.Sample.Converters"
x:TypeArguments="viewModels:MediaElementViewModel"
Expand All @@ -28,8 +29,9 @@
</Grid.RowDefinitions>
<toolkit:MediaElement
x:Name="MediaElement"
AndroidViewType="SurfaceView"
ShouldAutoPlay="True"
Source="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
Source="{x:Static constants:StreamingVideoUrls.BuckBunny}"
MetadataArtworkUrl="https://lh3.googleusercontent.com/pw/AP1GczNRrebWCJvfdIau1EbsyyYiwAfwHS0JXjbioXvHqEwYIIdCzuLodQCZmA57GADIo5iB3yMMx3t_vsefbfoHwSg0jfUjIXaI83xpiih6d-oT7qD_slR0VgNtfAwJhDBU09kS5V2T5ZML-WWZn8IrjD4J-g=w1792-h1024-s-no-gm"
MetadataTitle="Big Buck Bunny"
MetadataArtist="Blender Foundation"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel;
using CommunityToolkit.Maui.Core.Primitives;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Sample.Constants;
using CommunityToolkit.Maui.Sample.ViewModels.Views;
using CommunityToolkit.Maui.Views;
using Microsoft.Extensions.Logging;
Expand All @@ -17,7 +18,6 @@ public partial class MediaElementPage : BasePage<MediaElementViewModel>
const string resetSource = "Reset Source to null";
const string loadMusic = "Load Music";

const string buckBunnyMp4Url = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";
const string botImageUrl = "https://lh3.googleusercontent.com/pw/AP1GczNRrebWCJvfdIau1EbsyyYiwAfwHS0JXjbioXvHqEwYIIdCzuLodQCZmA57GADIo5iB3yMMx3t_vsefbfoHwSg0jfUjIXaI83xpiih6d-oT7qD_slR0VgNtfAwJhDBU09kS5V2T5ZML-WWZn8IrjD4J-g=w1792-h1024-s-no-gm";
const string hlsStreamTestUrl = "https://mtoczko.github.io/hls-test-streams/test-gap/playlist.m3u8";
const string hal9000AudioUrl = "https://github.com/prof3ssorSt3v3/media-sample-files/raw/master/hal-9000.mp3";
Expand Down Expand Up @@ -170,7 +170,7 @@ async void ChangeSourceClicked(Object sender, EventArgs e)
MediaElement.MetadataArtworkUrl = botImageUrl;
MediaElement.MetadataArtist = "Big Buck Bunny Album";
MediaElement.Source =
MediaSource.FromUri(buckBunnyMp4Url);
MediaSource.FromUri(StreamingVideoUrls.BuckBunny);
Copy link
Collaborator

@TheCodeTraveler TheCodeTraveler Apr 23, 2025

Choose a reason for hiding this comment

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

On MediaElementPage, when the Popup button is clicked, the MediaElement shown in the popup flickers.

Sometimes it shows the video. Sometimes it shows nothing:

Popup Flickering, Showing Nothing Popup Flickering, Showing MediaElement
Screenshot_1745451076 Screenshot_1745451075

Copy link
Collaborator

Choose a reason for hiding this comment

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

This may be a bug unrelated to this PR. If so, please open an Issue.

This also may be a bug on my emulator. If so, please let me know if you are unable to reproduce it on your Android device.

Copy link
Contributor

Choose a reason for hiding this comment

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

As to the flickering specifically that is a known issue with emulator and I cannot replicate it on any physical device on any API.

As to the blank screen I will try to replicate that. I have seen in some cases when navigating to page that video does not load. It looks like that happens with main too. So unrelated to this PR. But further testing is needed to confirm that. I will get that done. User selecting a video from a picker always loads which is weird if initial load is failing. I will have to investigate.

return;

case loadHls:
Expand Down Expand Up @@ -248,6 +248,7 @@ void DisplayPopup(object sender, EventArgs e)
MediaElement.Pause();
var popupMediaElement = new MediaElement
{
AndroidViewType = AndroidViewType.SurfaceView,
Source = MediaSource.FromResource("AppleVideo.mp4"),
HeightRequest = 600,
WidthRequest = 600,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Markup;
using CommunityToolkit.Maui.Sample.Constants;
using CommunityToolkit.Maui.Sample.ViewModels.Views;
using CommunityToolkit.Maui.Views;
using static CommunityToolkit.Maui.Markup.GridRowsColumns;

namespace CommunityToolkit.Maui.Sample.Pages.Views;

public partial class MediaElementTextureViewPage : BasePage<MediaElementTextureViewViewModel>
{
const int mediaElementHeight = 200;

public MediaElementTextureViewPage(MediaElementTextureViewViewModel viewModel) : base(viewModel)
{
Content = new Grid
{
RowSpacing = 24,
RowDefinitions = Rows.Define(
(Row.DescriptionLabel, 64),
(Row.ExplanationLabel, 64),
(Row.Video, mediaElementHeight * 1.75)), // Make the Video Row height slightly less than the height of two MediaElements to ensure overlap

Children =
{
new Label()
.Row(Row.DescriptionLabel)
.Text("This page demonstrates two overlaying MediaElements using TextureView on Android. The two videos below have the same height.")
.Font(size: 16),

new Label()
.Row(Row.ExplanationLabel)
.Text("It is not possible to have overlapping views using SurfaceView on Android; this is only possible using TextureView on Android.")
.Font(size: 16),

new TextureViewMediaElementInBorder(StreamingVideoUrls.BuckBunny, "https://lh3.googleusercontent.com/pw/AP1GczNRrebWCJvfdIau1EbsyyYiwAfwHS0JXjbioXvHqEwYIIdCzuLodQCZmA57GADIo5iB3yMMx3t_vsefbfoHwSg0jfUjIXaI83xpiih6d-oT7qD_slR0VgNtfAwJhDBU09kS5V2T5ZML-WWZn8IrjD4J-g=w1792-h1024-s-no-gm")
.Row(Row.Video)
.BackgroundColor(Colors.LightBlue)
.Top(),

new TextureViewMediaElementInBorder(StreamingVideoUrls.ElephantsDream,"https://lh3.googleusercontent.com/pw/AP1GczNRrebWCJvfdIau1EbsyyYiwAfwHS0JXjbioXvHqEwYIIdCzuLodQCZmA57GADIo5iB3yMMx3t_vsefbfoHwSg0jfUjIXaI83xpiih6d-oT7qD_slR0VgNtfAwJhDBU09kS5V2T5ZML-WWZn8IrjD4J-g=w1792-h1024-s-no-gm")
.Row(Row.Video)
.BackgroundColor(Colors.LightCoral)
.Bottom(),
}
};
}

enum Row { DescriptionLabel, ExplanationLabel, Video }

sealed class TextureViewMediaElementInBorder : Border
{
public TextureViewMediaElementInBorder(in string urlSource, in string metaDataArtworkUrl)
{
Stroke = Colors.Transparent;
StrokeThickness = 0;
Padding = 0;
HeightRequest = mediaElementHeight;
HorizontalOptions = LayoutOptions.Center;

Content = new MediaElement
{
AndroidViewType = AndroidViewType.TextureView,
Source = urlSource,
ShouldAutoPlay = true,
MetadataArtist = "Community Toolkit",
MetadataTitle = "Texture View",
MetadataArtworkUrl = metaDataArtworkUrl,
Margin = 0,
BackgroundColor = Colors.Transparent
}.Center();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:enableOnBackInvokedCallback="true"
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:enableOnBackInvokedCallback="true" android:hardwareAccelerated="true"
android:supportsRtl="true">

<meta-data android:name="com.google.android.geo.API_KEY" android:value="PASTE-YOUR-API-KEY-HERE" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Maui.Sample.Constants;

namespace CommunityToolkit.Maui.Sample.ViewModels.Views;

public partial class MediaElementCarouselViewViewModel : BaseViewModel
{
const string buckBunnyMp4Url = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";
const string elephantsDreamMp4Url = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4";
const string sintelMp4Url = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/Sintel.mp4";

public ObservableCollection<MediaElementDataSource> ItemSource { get; } =
[
new(new Uri(buckBunnyMp4Url), "Buck Bunny", 720, 1280),
new(new Uri(elephantsDreamMp4Url), "Elephants Dream", 720, 1280),
new(new Uri(sintelMp4Url), "Sintel", 546, 1280)
new(new Uri(StreamingVideoUrls.BuckBunny), "Buck Bunny", 720, 1280),
new(new Uri(StreamingVideoUrls.ElephantsDream), "Elephants Dream", 720, 1280),
new(new Uri(StreamingVideoUrls.Sintel), "Sintel", 546, 1280)
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Maui.Sample.Constants;

namespace CommunityToolkit.Maui.Sample.ViewModels.Views;

public partial class MediaElementCollectionViewViewModel : BaseViewModel
{
const string buckBunnyMp4Url = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";
const string elephantsDreamMp4Url = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4";
const string sintelMp4Url = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/Sintel.mp4";

public ObservableCollection<MediaElementDataSource> ItemSource { get; } =
[
new(new Uri(buckBunnyMp4Url), "Buck Bunny", 720, 1280),
new(new Uri(elephantsDreamMp4Url), "Elephants Dream", 720, 1280),
new(new Uri(sintelMp4Url), "Sintel", 546, 1280)
new(new Uri(StreamingVideoUrls.BuckBunny), "Buck Bunny", 720, 1280),
new(new Uri(StreamingVideoUrls.ElephantsDream), "Elephants Dream", 720, 1280),
new(new Uri(StreamingVideoUrls.Sintel), "Sintel", 546, 1280)
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace CommunityToolkit.Maui.Sample.ViewModels.Views;

public partial class MediaElementTextureViewViewModel : BaseViewModel
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@

public partial class MediaElementViewModel : BaseViewModel
{
public MediaElementViewModel()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public sealed partial class ViewsGalleryViewModel() : BaseGalleryViewModel(
SectionModel.Create<BasicMapsViewModel>("Maps Basic Page", Colors.Red, "A page demonstrating a basic example of .NET MAUI Maps"),
SectionModel.Create<LazyViewViewModel>("LazyView", Colors.Red, "LazyView is a view that allows you to load its children in a delayed manner."),
SectionModel.Create<MapsPinsViewModel>("Maps Pins Page", Colors.Red, "A page demonstrating .NET MAUI Maps with Pins."),
SectionModel.Create<MediaElementTextureViewViewModel>("MediaElement with TextureView", Colors.Red, "MediaElement can be used with a TextureView"),
SectionModel.Create<MediaElementViewModel>("MediaElement", Colors.Red, "MediaElement is a view for playing video and audio"),
SectionModel.Create<MediaElementCarouselViewViewModel>("MediaElement in CarouselView", Colors.Red, "MediaElement can be used inside a DataTemplate in a CarouselView"),
SectionModel.Create<MediaElementCollectionViewViewModel>("MediaElement in CollectionView", Colors.Red, "MediaElement can be used inside a DataTemplate in a CollectionView"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.Versioning;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Core.Handlers;
using CommunityToolkit.Maui.Views;

Expand All @@ -18,9 +19,14 @@ public static class AppBuilderExtensions
/// Initializes the .NET MAUI Community Toolkit MediaElement Library
/// </summary>
/// <param name="builder"><see cref="MauiAppBuilder"/> generated by <see cref="MauiApp"/>.</param>
/// <param name="options"></param>
/// <returns><see cref="MauiAppBuilder"/> initialized for <see cref="MediaElement"/>.</returns>
public static MauiAppBuilder UseMauiCommunityToolkitMediaElement(this MauiAppBuilder builder)
public static MauiAppBuilder UseMauiCommunityToolkitMediaElement(this MauiAppBuilder builder, Action<MediaElementOptions>? options = null)
{
// Update the default MediaElementOptions for MediaElement if Action is not null
options?.Invoke(new MediaElementOptions(builder));

// Perform Handler configuration
builder.ConfigureMauiHandlers(h =>
{
h.AddHandler<MediaElement, MediaElementHandler>();
Expand All @@ -29,6 +35,7 @@ public static MauiAppBuilder UseMauiCommunityToolkitMediaElement(this MauiAppBui
#if ANDROID
builder.Services.AddSingleton<Media.Services.MediaControlsService>();
#endif

return builder;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[assembly: XmlnsDefinition(Constants.XamlNamespace, Constants.CommunityToolkitNamespacePrefix + nameof(CommunityToolkit.Maui.Converters))]
[assembly: XmlnsDefinition(Constants.XamlNamespace, Constants.CommunityToolkitNamespacePrefix + nameof(CommunityToolkit.Maui.Core))]
[assembly: XmlnsDefinition(Constants.XamlNamespace, Constants.CommunityToolkitNamespacePrefix + nameof(CommunityToolkit.Maui.Core.Primitives))]
[assembly: XmlnsDefinition(Constants.XamlNamespace, Constants.CommunityToolkitNamespacePrefix + nameof(CommunityToolkit.Maui.Core.Handlers))]
[assembly: XmlnsDefinition(Constants.XamlNamespace, Constants.CommunityToolkitNamespacePrefix + nameof(CommunityToolkit.Maui.Core.Views))]
[assembly: XmlnsDefinition(Constants.XamlNamespace, Constants.CommunityToolkitNamespacePrefix + nameof(CommunityToolkit.Maui.Views))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,15 @@
<PackageReference Include="Xamarin.AndroidX.Media3.ExoPlayer.Rtsp" Version="1.5.0" />
<PackageReference Include="Xamarin.AndroidX.Media3.ExoPlayer.Hls" Version="1.5.0" />
<PackageReference Include="Xamarin.AndroidX.Media3.ExoPlayer.Dash" Version="1.5.0" />
<PackageReference Include="Xamarin.AndroidX.Media3.Session" Version="1.5.0" />
<PackageReference Include="Xamarin.AndroidX.Media3.Session" Version="1.5.0" />

<!--include the textureview.xml needed for creating textureview in android-->
<Folder Include="Platforms\Android\Resources\layout\" />
<AndroidResource Include="Platforms\Android\Resources\layout\textureview.xml" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="[$(MauiPackageVersion),$(NextMauiPackageVersion))"/>
<PackageReference Include="Microsoft.Maui.Controls" Version="[$(MauiPackageVersion),$(NextMauiPackageVersion))" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" Condition=" '$(Configuration)'=='Release' " PrivateAssets="All" />
</ItemGroup>

Expand All @@ -76,4 +80,5 @@
<LogicalName>ResourceDictionary.windows.xaml</LogicalName>
</EmbeddedResource>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ public partial class MediaElementHandler : ViewHandler<MediaElement, MauiMediaEl
/// <param name="mediaElement">The associated <see cref="MediaElement"/> instance.</param>
public static void ShouldLoopPlayback(MediaElementHandler handler, MediaElement mediaElement)
{
handler.mediaManager?.UpdateShouldLoopPlayback();
handler.MediaManager?.UpdateShouldLoopPlayback();
}


protected override MauiMediaElement CreatePlatformView()
{
mediaManager ??= new(MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} cannot be null"),
MediaManager ??= new(MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} cannot be null"),
VirtualView,
Dispatcher.GetForCurrentThread() ?? throw new InvalidOperationException($"{nameof(IDispatcher)} cannot be null"));

var (_, playerView) = mediaManager.CreatePlatformView();
var (_, playerView) = MediaManager.CreatePlatformView(VirtualView.AndroidViewType);
return new(Context, playerView);
}

Expand Down
Loading