Maui
Maui copied to clipboard
[BUG] MediaElement ShouldAutoPlay="True" does not work (tested on Windows 11)
Is there an existing issue for this?
- [X] I have searched the existing issues
Did you read the "Reporting a bug" section on Contributing file?
- [X] I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
Current Behavior
The ShouldAutoPlay attribute of toolkit:MediaElement does not cause media to auto play when set to True as follows:
<toolkit:MediaElement
ShouldAutoPlay="True"
ShouldLoopPlayback="True"
Source="embed://WhiteNoise.ogg" />
All other aspects of the MediaElement work as expected in this example.
Expected Behavior
The media should auto play.
Steps To Reproduce
I have created a simple example repository showing this behavior: https://github.com/dustuu/HelloMaui
The relevant lines can be found here: https://github.com/dustuu/HelloMaui/blob/357a8fc19301366bbe83db7eded5bc84ae7eff3b/HelloMaui/MainPage.xaml#L20
This is a very minimal application with no custom configuration done. It was built from the default Visual Studio MAUI template with no significant changes beyond this MediaElement addition.
Link to public reproduction project repository
https://github.com/dustuu/HelloMaui
Environment
- .NET MAUI CommunityToolkit: 2.0.0
- OS: Microsoft Windows 11 Pro Version 10.0.22621 Build 22621
- .NET MAUI: 7.0.92
Anything else?
No response
The following workaround works for me ...
In your xaml, give the MediaElement an x:Name ...
<toolkit:MediaElement
x:Name="TheMediaElement"
Source="embed://some_lovely_video.mp4"
ShouldAutoPlay="True"
ShouldLoopPlayback="True"
/>
Then in your code-behind, hook the Loaded event and call the Play method from there ...
public partial class MyLovelyPagePage : ContentPage
{
public MyLovelyPagePage ()
{
InitializeComponent();
Loaded += MyLovelyPagePage_Loaded;
}
private void MyLovelyPagePage_Loaded(object? sender, EventArgs e)
{
TheMediaElement.Play();
}
}
Tested media element sample app and it works with autoplay enabled. @vhugogarcia
Sample:
https://github.com/CommunityToolkit/Maui/assets/4167863/a88f46b9-4c1a-4412-9edd-af023794161a
@vhugogarcia
Thanks @ne0rrmatrix for testing it and confirming it is working on the latest release. I'm closing this issue.