0
votes

I am Using Xamarin forms for play some music , when I lock my phone, the audio will stop. how should I Handel ? I do not want music pause

{

MediaElement KeepScreenOn="True" x:FieldModifier="Public" Source="Voice.mp3" AutoPlay="False" ShowsPlaybackControls="True" x:Name="MyMedia"

}

1

1 Answers

0
votes

MediaElement is currently experimental, I cannot found this feature play the music when lock screen, but you can use CrossMediaManager, It is support the play the music when lock screen by default.

Just Used a Button to invoke the play method.

 <Button Text="play" Clicked="Button_Clicked"></Button>
 private async void Button_Clicked(object sender, EventArgs e)
        {
            await CrossMediaManager.Current.Play("https://ia800806.us.archive.org/15/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3");
        }

Note:when add CrossMediaManager.Current.Init(this); in your OnCreate method of MainActivity.cs. Please do not forget the set the Target Framework to Android 9.0 or later.