MediaElement does not reproduce sound effect

I want to play a sound effect when the user clicks.

I have a file beep.mp3added to the project (not in the folder). In it, the properties that I see are Build Actionset toContent

and this MediaElement:

<MediaElement Name="beep" Source="beep.mp3" Volume="1" AutoPlay="False"/>

after that I do not hear any sounds:

beep.Play();
+4
source share
2 answers

I built a simple example (following your code) and everything should be fine. Just to make sure everything is in order, I will do a few checks as described below:
In XAML:

<Button x:Name="myButton" VerticalAlignment="Cener" Content="BEEP"/>
<MediaElement Name="beep" Source="beep.mp3" Volume="1" AutoPlay="False" MediaFailed="beep_MediaFailed" MediaOpened="beep_MediaOpened"/>

Code behind:

public MainPage()
{
   InitializeComponent();

   myButton.Click += (sender, e) => { beep.Play(); };
   // this below checks if your file exists 
   if (Application.GetResourceStream(new Uri("beep.mp3", UriKind.Relative)) == null)
        MessageBox.Show("File not Exists!");
}

private void beep_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
   //  Show Message when opening failed 
   MessageBox.Show("There was an error: " + e.ErrorException.Message);
}

private void beep_MediaOpened(object sender, RoutedEventArgs e)
{
   // as it subscribed in xaml, juts after opening the App you should hear beep
   beep.Play();
   MessageBox.Show("Media opened");
}

- , beep.mp3 (, ). , . (Failed/Opened). , .mp3.
, , , beep.Play() InitializeComponent, , , - , (, , , , ).

, , , WiredPraire ( ) SoundEffect .

, .

0

- Silverlight. "", "/" "" .

0

Source: https://habr.com/ru/post/1524898/


All Articles