Skip to content

Commit d578bb4

Browse files
committed
Added media element sample page and fixed small bug on Android
1 parent e443cf4 commit d578bb4

9 files changed

Lines changed: 585 additions & 5 deletions

File tree

samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public partial class AppShell : Shell
125125
CreateViewModelMapping<LazyViewPage, LazyViewViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
126126
CreateViewModelMapping<MapsPinsPage, MapsPinsViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
127127
CreateViewModelMapping<MediaElementPage, MediaElementViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
128+
CreateViewModelMapping<MediaElementFromStreamPage, MediaElementFromStreamViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
128129
CreateViewModelMapping<MediaElementCarouselViewPage, MediaElementCarouselViewViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
129130
CreateViewModelMapping<MediaElementCollectionViewPage, MediaElementCollectionViewViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),
130131
CreateViewModelMapping<MediaElementMultipleWindowsPage, MediaElementMultipleWindowsViewModel, ViewsGalleryPage, ViewsGalleryViewModel>(),

samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
<WindowsSdkPackageVersion>10.0.19041.56</WindowsSdkPackageVersion>
3333
</PropertyGroup>
3434

35-
<PropertyGroup Condition="'$(Configuration)' == 'Release'
36-
AND $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) != 'windows'">
35+
<PropertyGroup Condition="'$(Configuration)' == 'Release'&#xD;&#xA; AND $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) != 'windows'">
3736
<!-- Windows error NETSDK1102: Optimizing assemblies for size is not supported for the selected publish configuration. -->
3837
<PublishAot>false</PublishAot>
3938
<PublishTrimmed>true</PublishTrimmed>
@@ -87,6 +86,12 @@
8786
<ProjectReference Include="..\..\src\CommunityToolkit.Maui.Maps\CommunityToolkit.Maui.Maps.csproj" />
8887
</ItemGroup>
8988

89+
<ItemGroup>
90+
<MauiXaml Update="Pages\Views\MediaElement\MediaElementFromStreamPage.xaml">
91+
<Generator>MSBuild:Compile</Generator>
92+
</MauiXaml>
93+
</ItemGroup>
94+
9095
<PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))=='windows' and $(Configuration) == 'Release'">
9196
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
9297
</PropertyGroup>

samples/CommunityToolkit.Maui.Sample/MauiProgram.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ static void RegisterViewsAndViewModels(in IServiceCollection services)
252252
services.AddTransientWithShellRoute<LazyViewPage, LazyViewViewModel>();
253253
services.AddTransientWithShellRoute<MapsPinsPage, MapsPinsViewModel>();
254254
services.AddTransientWithShellRoute<MediaElementPage, MediaElementViewModel>();
255-
services.AddTransientWithShellRoute<MediaElementCarouselViewPage, MediaElementCarouselViewViewModel>();
255+
services.AddTransientWithShellRoute<MediaElementFromStreamPage, MediaElementFromStreamViewModel>();
256+
services.AddTransientWithShellRoute<MediaElementCarouselViewPage, MediaElementCarouselViewViewModel>();
256257
services.AddTransientWithShellRoute<MediaElementCollectionViewPage, MediaElementCollectionViewViewModel>();
257258
services.AddTransientWithShellRoute<MediaElementMultipleWindowsPage, MediaElementMultipleWindowsViewModel>();
258259
services.AddTransientWithShellRoute<PopupsPage, PopupsViewModel>();
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<pages:BasePage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:pages="clr-namespace:CommunityToolkit.Maui.Sample.Pages"
5+
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
6+
xmlns:constants="clr-namespace:CommunityToolkit.Maui.Sample.Constants"
7+
xmlns:viewModels="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Views"
8+
xmlns:converters="clr-namespace:CommunityToolkit.Maui.Sample.Converters"
9+
x:TypeArguments="viewModels:MediaElementFromStreamViewModel"
10+
x:DataType="viewModels:MediaElementFromStreamViewModel"
11+
x:Class="CommunityToolkit.Maui.Sample.Pages.Views.MediaElementFromStreamPage"
12+
Title="MediaElement">
13+
<pages:BasePage.Resources>
14+
<converters:SecondsToStringConverter x:Key="SecondsToStringConverter" />
15+
</pages:BasePage.Resources>
16+
17+
<ScrollView>
18+
<Grid>
19+
<Grid.RowDefinitions>
20+
<RowDefinition Height="220" />
21+
<RowDefinition Height="50" />
22+
<RowDefinition Height="Auto" />
23+
<RowDefinition Height="Auto" />
24+
<RowDefinition Height="Auto" />
25+
<RowDefinition Height="Auto" />
26+
<RowDefinition Height="Auto" />
27+
<RowDefinition Height="Auto" />
28+
<RowDefinition Height="Auto" />
29+
</Grid.RowDefinitions>
30+
<toolkit:MediaElement
31+
x:Name="MediaElement"
32+
ShouldAutoPlay="True"
33+
MediaEnded="OnMediaEnded"
34+
MediaFailed="OnMediaFailed"
35+
MediaOpened="OnMediaOpened"
36+
PositionChanged="OnPositionChanged"
37+
StateChanged="OnStateChanged"
38+
SeekCompleted="OnSeekCompleted"/>
39+
<HorizontalStackLayout Grid.Row="1" Padding="0,0,0,15">
40+
<Label HorizontalOptions="Center" x:DataType="toolkit:MediaElement">
41+
<Label.Text>
42+
<MultiBinding StringFormat="{}Current State: {0} - Dimensions: {1}x{2}">
43+
<Binding Path="CurrentState" Source="{x:Reference MediaElement}" />
44+
<Binding Path="MediaWidth" Source="{x:Reference MediaElement}" />
45+
<Binding Path="MediaHeight" Source="{x:Reference MediaElement}" />
46+
</MultiBinding>
47+
</Label.Text>
48+
</Label>
49+
50+
</HorizontalStackLayout>
51+
52+
<Grid Grid.Row="2" Padding="0,10,0,10" ColumnDefinitions="*,*,*,*,*" ColumnSpacing="5">
53+
<Button Grid.Column="0" Text="Play" Clicked="OnPlayClicked" />
54+
<Button Grid.Column="1" Text="Pause" Clicked="OnPauseClicked" />
55+
<Button Grid.Column="2" Text="Stop" Clicked="OnStopClicked" />
56+
<Button Grid.Column="3" Text="Mute" Clicked="OnMuteClicked">
57+
<Button.Triggers>
58+
<DataTrigger TargetType="Button"
59+
x:DataType="toolkit:MediaElement"
60+
Binding="{Binding ShouldMute, Source={x:Reference MediaElement}}"
61+
Value="True">
62+
<Setter Property="Text" Value="Unmute" />
63+
</DataTrigger>
64+
<DataTrigger TargetType="Button"
65+
x:DataType="toolkit:MediaElement"
66+
Binding="{Binding ShouldMute, Source={x:Reference MediaElement}}"
67+
Value="False">
68+
<Setter Property="Text" Value="Mute" />
69+
</DataTrigger>
70+
</Button.Triggers>
71+
</Button>
72+
<Button Grid.Column="4" Text="Popup" Clicked="DisplayPopup" />
73+
</Grid>
74+
75+
<Slider
76+
x:Name="PositionSlider"
77+
Grid.Row="3"
78+
MinimumTrackColor="Gray"
79+
DragStarted="Slider_DragStarted"
80+
DragCompleted="Slider_DragCompleted"/>
81+
82+
<Grid Grid.Row="4" HorizontalOptions="Fill">
83+
<Label HorizontalOptions="Start" x:DataType="toolkit:MediaElement" Text="{Binding Position, Source={x:Reference MediaElement}, Converter={StaticResource SecondsToStringConverter}}" />
84+
<Label HorizontalOptions="End" HorizontalTextAlignment="End" x:DataType="toolkit:MediaElement" Text="{Binding Duration, Source={x:Reference MediaElement}, Converter={StaticResource SecondsToStringConverter}}" />
85+
</Grid>
86+
87+
<VerticalStackLayout Padding="0,20,0,0" Grid.Row="5" Spacing="5">
88+
<HorizontalStackLayout Spacing="5" HorizontalOptions="Center">
89+
<Button Text="Open Video" Clicked="OnOpenVideoClicked" HorizontalOptions="Fill" />
90+
91+
<Button Text="Record Video" Clicked="OnRecordVideoClicked" HorizontalOptions="Fill" />
92+
</HorizontalStackLayout>
93+
<Button Text="Change Aspect" Clicked="ChangeAspectClicked" />
94+
</VerticalStackLayout>
95+
96+
<HorizontalStackLayout Grid.Row="6" Padding="0,10,0,10" Spacing="5">
97+
<Label Margin="20,10" x:DataType="toolkit:MediaElement">
98+
<Label.FormattedText>
99+
<FormattedString>
100+
<Span Text="Speed:" />
101+
<Span Text="{Binding Source={x:Reference MediaElement}, Path=Speed}" />
102+
</FormattedString>
103+
</Label.FormattedText>
104+
</Label>
105+
106+
<Button Text="-" Clicked="OnSpeedMinusClicked" />
107+
<Button Text="+" Clicked="OnSpeedPlusClicked" />
108+
</HorizontalStackLayout>
109+
110+
<HorizontalStackLayout Grid.Row="7" Padding="0,10,0,10" Spacing="5">
111+
<Label Margin="20,10" x:DataType="toolkit:MediaElement">
112+
<Label.FormattedText>
113+
<FormattedString>
114+
<Span Text="Volume:" />
115+
<Span Text="{Binding Source={x:Reference MediaElement}, Path=Volume, StringFormat='{}{0:P0}'}" />
116+
</FormattedString>
117+
</Label.FormattedText>
118+
</Label>
119+
120+
<Button Text="-" Clicked="OnVolumeMinusClicked" />
121+
<Button Text="+" Clicked="OnVolumePlusClicked" />
122+
</HorizontalStackLayout>
123+
124+
<VerticalStackLayout Grid.Row="8">
125+
<HorizontalStackLayout Padding="0,0,0,10">
126+
<Switch
127+
Margin="0,0,5,0"
128+
ThumbColor="White"
129+
OnColor="LimeGreen"
130+
x:DataType="toolkit:MediaElement"
131+
IsToggled="{Binding Source={x:Reference MediaElement}, Path=ShouldShowPlaybackControls}" />
132+
<Label
133+
VerticalOptions="Center"
134+
Text="Show playback controls" />
135+
</HorizontalStackLayout>
136+
<HorizontalStackLayout Padding="0,0,0,10">
137+
<Switch
138+
Margin="0,0,5,0"
139+
ThumbColor="White"
140+
OnColor="LimeGreen"
141+
x:DataType="toolkit:MediaElement"
142+
IsToggled="{Binding Source={x:Reference MediaElement}, Path=ShouldLoopPlayback}" />
143+
<Label
144+
VerticalOptions="Center"
145+
Text="Loop media" />
146+
</HorizontalStackLayout>
147+
<HorizontalStackLayout>
148+
<Switch
149+
Margin="0,0,5,0"
150+
ThumbColor="White"
151+
OnColor="LimeGreen"
152+
x:DataType="toolkit:MediaElement"
153+
IsToggled="{Binding Source={x:Reference MediaElement}, Path=ShouldKeepScreenOn}" />
154+
<Label
155+
VerticalOptions="Center"
156+
Text="Keep screen on" />
157+
</HorizontalStackLayout>
158+
</VerticalStackLayout>
159+
</Grid>
160+
</ScrollView>
161+
</pages:BasePage>

0 commit comments

Comments
 (0)