3

I ran into this issue multiple times but until this current task that I'm doing it didn't worry me too much. But now I really need to remove autoplay because this audio must only play on user interaction and not at the beginning of the page load.

MediaPlayer CurrentState property and cannot be changed or assigned on screen loading because it is readonly. This is very annoying and IDK why it is not working. AutoPlay="false" and the sound still plays at the start of page loading.

My element is invisible due to it only having to play sound and no one needs to see it. If you have another way to do this please suggest in the comments and I will try.

<xct:MediaElement x:Name="mediaPlayer"
                  Source="{Binding WordSoundSource}"
                  IsVisible="false"
                  AutoPlay="false"/>

Update
If I add in my constructor mediaPlayer.Stop() to stop the element from playing at the start then on the play event of a user tapping nothing plays this is extremely annoying.

This is how I handle the mediaPlayer.Play() on user tapping an image which has Image.GestureRecognizers && TapGestureRecognizer

<Image Source="sound_icon.png"
       VerticalOptions="CenterAndExpand" 
       HorizontalOptions="EndAndExpand"
       HeightRequest="40"
       WidthRequest="40"
       Margin="0, 10, 80, 0">
           <Image.GestureRecognizers>
               <TapGestureRecognizer x:Name="ReplaySound"
                                     NumberOfTapsRequired="1"
                                     Tapped="ReplaySound_Tapped"/>
           </Image.GestureRecognizers>
</Image>
private void ReplaySound_Tapped(object sender, EventArgs e)
{
    if (mediaPlayer.CurrentState == MediaElementState.Stopped || 
       mediaPlayer.CurrentState == MediaElementState.Paused)
    {
        mediaPlayer.Play();
    }
}

There is also a post issues #1715 on GitHub about this being a bug, but it's almost a year ago... Am I wondering if this is the same thing?

Thanks in advance.

  • Did you try settin Speed to 0 as suggested in the bug? – Jason Oct 06 '22 at 12:06
  • Yeah, that does work, but it's a weird workout and I have a solution. I really wanted to know if there are any other solutions that could potentially have popped up over the year that the bugged has existed. – ThisQRequiresASpecialist Oct 06 '22 at 12:11
  • Nope no new solution, after reading your question I went back to the old repo of mine just to confirm and I still had the same issue, With the only solution being as given below – FreakyAli Oct 06 '22 at 12:13

1 Answers1

1

I had this issue last year the only possible way to solve it is setting Speed to 0 by default and then setting it to 1 when you need to play.

<MediaElement 
       x:Name ="vidPlayer"
       Source="ms-appx:///XamarinForms101UsingEmbeddedImages.mp4"
       Speed="0"   
       ShowsPlaybackControls="True"
       .
       . 
       .
        />

and then Change it to 1 when you play:

vidPlayer.Speed= 1.0;
FreakyAli
  • 13,349
  • 3
  • 23
  • 63
  • Bruh I understand it's a weird workaround so I totally feel you. To avoid such issues in MAUI I am planning to make my own controls from scratch, I have already made a bunch of them you can check it here if you are ever interested: https://github.com/FreakyAli/Maui.FreakyControls Video/AudioPlayer is something I have my sights on next or may be you can drop feature requests and I will see what I can do :) – FreakyAli Oct 06 '22 at 12:16