Skip to content

Commit 940c549

Browse files
committed
MAUI: Make samples work on WinUI
1 parent 230fa50 commit 940c549

File tree

11 files changed

+70
-35
lines changed

11 files changed

+70
-35
lines changed

samples/LibVLCSharp.MAUI.Sample/AppShell.xaml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,5 @@
55
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
66
xmlns:local="clr-namespace:LibVLCSharp.MAUI.Sample"
77
Shell.FlyoutBehavior="Disabled">
8-
9-
<ShellContent
10-
Title="Home"
11-
ContentTemplate="{DataTemplate local:MainPage}"
12-
Route="MainPage" />
13-
8+
<ShellContent ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" />
149
</Shell>

samples/LibVLCSharp.MAUI.Sample/LibVLCSharp.MAUI.Sample.csproj

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net8.0-android;net8.0-ios</TargetFrameworks>
5+
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
56
<OutputType>Exe</OutputType>
67
<RootNamespace>LibVLCSharp.MAUI.Sample</RootNamespace>
78
<UseMaui>true</UseMaui>
89
<SingleProject>true</SingleProject>
910
<ImplicitUsings>enable</ImplicitUsings>
10-
1111
<!-- Display name -->
12-
<ApplicationTitle>LibVLCSharp.MAUI.Sample</ApplicationTitle>
13-
12+
<ApplicationTitle>LibVLCSharp.MAUI.SampleX</ApplicationTitle>
13+
<WindowsPackageType>None</WindowsPackageType>
1414
<!-- App Identifier -->
15-
<ApplicationId>com.companyname.libvlcsharp.maui.sample</ApplicationId>
15+
<ApplicationId>com.companyname.libvlcsharp.maui.samplex</ApplicationId>
1616
<ApplicationIdGuid>49861f9e-48d3-45c0-b6a8-f591ad6d2010</ApplicationIdGuid>
1717

1818
<!-- Versions -->
@@ -24,7 +24,7 @@
2424
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
2525
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
2626
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
27-
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
27+
<DefaultLanguage>en</DefaultLanguage>
2828
</PropertyGroup>
2929

3030
<ItemGroup>
@@ -64,4 +64,7 @@
6464
<ItemGroup Condition="$(TargetFramework.Contains('-ios'))">
6565
<PackageReference Include="VideoLAN.LibVLC.iOS" Version="3.3.18" />
6666
</ItemGroup>
67+
<ItemGroup Condition="$(TargetFramework.Contains('-windows'))">
68+
<PackageReference Include="VideoLAN.LibVLC.Windows" Version="3.0.21" />
69+
</ItemGroup>
6770
</Project>

samples/LibVLCSharp.MAUI.Sample/MainPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
<local:MainViewModel />
99
</ContentPage.BindingContext>
1010

11-
<shared:VideoView x:Name="VideoView" MediaPlayer="{Binding MediaPlayer}" MediaPlayerChanged="VideoView_MediaPlayerChanged" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
11+
<shared:VideoView x:Name="VideoView" MediaPlayer="{Binding MediaPlayer}" MediaPlayerChanged="VideoView_MediaPlayerChanged" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" HandlerChanged="VideoView_HandlerChanged" />
1212

1313
</ContentPage>

samples/LibVLCSharp.MAUI.Sample/MainPage.xaml.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public MainPage()
1212
protected override void OnAppearing()
1313
{
1414
base.OnAppearing();
15+
#if !WINDOWS
1516
((MainViewModel)BindingContext).OnAppearing();
17+
#endif
1618
}
1719

1820
protected override void OnDisappearing()
@@ -25,5 +27,18 @@ private void VideoView_MediaPlayerChanged(object sender, MediaPlayerChangedEvent
2527
{
2628
((MainViewModel)BindingContext).OnVideoViewInitialized();
2729
}
30+
31+
private void VideoView_HandlerChanged(object sender, EventArgs e)
32+
{
33+
#if WINDOWS
34+
var windowsView = ((LibVLCSharp.Platforms.Windows.VideoView)VideoView.Handler.PlatformView);
35+
36+
windowsView.Initialized += (s, e) =>
37+
{
38+
((MainViewModel)BindingContext).Initialize(e.SwapChainOptions);
39+
((MainViewModel)BindingContext).OnAppearing();
40+
};
41+
#endif
42+
}
2843
}
2944
}

samples/LibVLCSharp.MAUI.Sample/MainViewModel.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public class MainViewModel : INotifyPropertyChanged
99

1010
public MainViewModel()
1111
{
12+
#if !WINDOWS
1213
Initialize();
14+
#endif
1315
}
1416

1517
private LibVLC LibVLC { get; set; }
@@ -33,9 +35,9 @@ private void Set<T>(string propertyName, ref T field, T value)
3335
}
3436
}
3537

36-
private void Initialize()
38+
internal void Initialize(string[] swapchainOptions = null)
3739
{
38-
LibVLC = new LibVLC(enableDebugLogs: true);
40+
LibVLC = new LibVLC(enableDebugLogs: true, swapchainOptions);
3941
using var media = new Media(LibVLC, new Uri("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"));
4042

4143
MediaPlayer = new Shared.MediaPlayer(LibVLC)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"profiles": {
33
"Windows Machine": {
4-
"commandName": "MsixPackage",
5-
"nativeDebugging": false
4+
"commandName": "Project",
5+
"nativeDebugging": true
66
}
77
}
88
}

samples/MAUI/LibVLCSharp.MAUI.Sample.MediaElement/LibVLCSharp.MAUI.Sample.MediaElement.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>net8.0-android;net8.0-ios</TargetFrameworks>
4-
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
5-
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
4+
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041</TargetFrameworks>
65
<OutputType>Exe</OutputType>
76
<RootNamespace>LibVLCSharp.MAUI.Sample.MediaElement</RootNamespace>
87
<UseMaui>true</UseMaui>
98
<SingleProject>true</SingleProject>
109
<ImplicitUsings>enable</ImplicitUsings>
1110
<LangVersion>latest</LangVersion>
11+
<WindowsPackageType>None</WindowsPackageType>
1212
<!-- Display name -->
1313
<ApplicationTitle>LibVLCSharp.MAUI.Sample.MediaElement</ApplicationTitle>
1414
<!-- App Identifier -->
@@ -21,9 +21,7 @@
2121
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
2222
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
2323
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
24-
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
2524
<GenerateDocumentationFile>True</GenerateDocumentationFile>
26-
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.19041.0</SupportedOSPlatformVersion>
2725
</PropertyGroup>
2826
<ItemGroup>
2927
<!-- App Icon -->
@@ -49,6 +47,9 @@
4947
<ItemGroup Condition="$(TargetFramework.Contains('-ios'))">
5048
<PackageReference Include="VideoLAN.LibVLC.iOS" Version="3.3.18" />
5149
</ItemGroup>
50+
<ItemGroup Condition="$(TargetFramework.Contains('-windows'))">
51+
<PackageReference Include="VideoLAN.LibVLC.Windows" Version="3.0.21" />
52+
</ItemGroup>
5253
<ItemGroup>
5354
<ProjectReference Include="..\..\..\src\LibVLCSharp.MAUI\LibVLCSharp.MAUI.csproj" />
5455
<ProjectReference Include="..\..\..\src\LibVLCSharp\LibVLCSharp.csproj" />

samples/MAUI/LibVLCSharp.MAUI.Sample.MediaElement/MainPage.xaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
xmlns:local="clr-namespace:LibVLCSharp.MAUI.Sample.MediaElement"
55
xmlns:vlc="clr-namespace:LibVLCSharp.MAUI;assembly=LibVLCSharp.MAUI"
66
x:Class="LibVLCSharp.MAUI.Sample.MediaElement.MainPage"
7-
Appearing="OnAppearing"
8-
Disappearing="OnDisappearing">
7+
Appearing="ContentPage_Appearing"
8+
Disappearing="ContentPage_Disappearing">
99

1010
<ContentPage.BindingContext>
1111
<local:MainViewModel />
1212
</ContentPage.BindingContext>
1313

14-
<vlc:MediaPlayerElement MediaPlayer="{Binding MediaPlayer}" LibVLC="{Binding LibVLC}" EnableRendererDiscovery="True" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
14+
<vlc:MediaPlayerElement x:Name="MediaPlayerElement" MediaPlayer="{Binding MediaPlayer}" LibVLC="{Binding LibVLC}" EnableRendererDiscovery="True" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
1515
<vlc:MediaPlayerElement.PlaybackControls>
1616
<vlc:PlaybackControls
17-
IsAspectRatioButtonVisible="True" IsSeekButtonVisible="True" IsCastButtonVisible="True" />
17+
IsAspectRatioButtonVisible="True" IsSeekButtonVisible="True" IsCastButtonVisible="True"/>
1818
</vlc:MediaPlayerElement.PlaybackControls>
1919

2020
</vlc:MediaPlayerElement>

samples/MAUI/LibVLCSharp.MAUI.Sample.MediaElement/MainPage.xaml.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,36 @@ public partial class MainPage : ContentPage
1313
public MainPage()
1414
{
1515
InitializeComponent();
16+
MediaPlayerElement.PlaybackControls.VideoView.HandlerChanged += VideoView_HandlerChanged;
1617
}
1718

18-
void OnAppearing(object sender, System.EventArgs e)
19+
private void VideoView_HandlerChanged(object sender, EventArgs e)
20+
{
21+
#if WINDOWS
22+
var windowsView = ((LibVLCSharp.Platforms.Windows.VideoView)MediaPlayerElement.PlaybackControls.VideoView.Handler.PlatformView);
23+
24+
windowsView.Initialized += (s, e) =>
25+
{
26+
((MainViewModel)BindingContext).OnAppearing(e.SwapChainOptions);
27+
};
28+
#endif
29+
}
30+
31+
private void ContentPage_Appearing(object sender, EventArgs e)
1932
{
2033
base.OnAppearing();
34+
#if !WINDOWS
2135
((MainViewModel)BindingContext).OnAppearing();
36+
#endif
2237
}
2338

24-
void OnDisappearing(object sender, System.EventArgs e)
39+
private void ContentPage_Disappearing(object sender, EventArgs e)
2540
{
2641
base.OnDisappearing();
42+
43+
// this is a hack to get the mediaplayer/libvlc objects databinding disabled before disposing of them in the viewmodel OnDisappearing function. The MediaElement control and MAUI lifecycle shutdown procedure don't play nice together, so sometimes, on destroy, media element events are being unsubscribed on an already disposed mediaplayer instance, causing a memory violation exception.
44+
MediaPlayerElement.MediaPlayer = null;
45+
MediaPlayerElement.LibVLC = null;
2746
((MainViewModel)BindingContext).OnDisappearing();
2847
}
2948
}

samples/MAUI/LibVLCSharp.MAUI.Sample.MediaElement/MainViewModel.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ public LibVLCSharp.Shared.MediaPlayer MediaPlayer
4646
/// <summary>
4747
/// Initialize LibVLC and playback when page appears
4848
/// </summary>
49-
public void OnAppearing()
49+
public void OnAppearing(string[] swapchainOptions = null)
5050
{
5151
if (LibVLC == null)
5252
{
53-
LibVLC = new LibVLC(enableDebugLogs: true);
53+
LibVLC = new LibVLC(enableDebugLogs: true, swapchainOptions);
5454
}
5555

5656
if (MediaPlayer == null)
@@ -70,11 +70,11 @@ public void OnAppearing()
7070
/// </summary>
7171
public void OnDisappearing()
7272
{
73-
MediaPlayer?.Dispose();
74-
MediaPlayer = null;
75-
76-
LibVLC?.Dispose();
77-
LibVLC = null;
73+
_mediaPlayer?.Stop();
74+
_mediaPlayer?.Dispose();
75+
_mediaPlayer = null;
76+
_libVLC?.Dispose();
77+
_libVLC = null;
7878
}
7979

8080
/// <summary>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"profiles": {
33
"Windows Machine": {
4-
"commandName": "MsixPackage",
5-
"nativeDebugging": false
4+
"commandName": "Project",
5+
"nativeDebugging": true
66
}
77
}
88
}

0 commit comments

Comments
 (0)