Maui icon indicating copy to clipboard operation
Maui copied to clipboard

[BUG] MediaElement ShouldAutoPlay="True" does not work (tested on Windows 11)

Open dustuu opened this issue 2 years ago • 1 comments

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

dustuu avatar Sep 14 '23 16:09 dustuu

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();
    }
}

Keflon avatar Feb 13 '24 15:02 Keflon

Tested media element sample app and it works with autoplay enabled. @vhugogarcia

ne0rrmatrix avatar Jun 17 '24 21:06 ne0rrmatrix

Sample:

https://github.com/CommunityToolkit/Maui/assets/4167863/a88f46b9-4c1a-4412-9edd-af023794161a

@vhugogarcia

ne0rrmatrix avatar Jun 17 '24 21:06 ne0rrmatrix

Thanks @ne0rrmatrix for testing it and confirming it is working on the latest release. I'm closing this issue.

vhugogarcia avatar Jun 18 '24 03:06 vhugogarcia