Hello,
MediaElement
is a page control that must be on the page to function.
If you don't want to declare MediaElement
in xaml, you could refer to the following code:
//in xaml
<VerticalStackLayout x:Name="myStack"
...
</VerticalStackLayout>
// code-behind
MediaElement mediaPlayer;
mediaPlayer = new MediaElement()
{
Speed = 1,
IsVisible = false,
Source = MediaSource.FromResource("alert.mp3"), /* Resouces\Raw\Alert.mp3 */
ShouldAutoPlay = false,
ShouldShowPlaybackControls = false,
ShouldMute = false,
Volume = 0.95
};
myStack.Add(mediaPlayer);
mediaPlayer.Play();
myStack.Remove(mediaPlayer);
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.