private void button1_Click(object sender, RoutedEventArgs e)
{
MediaElement Lala =
((MediaElement)App.Current.Resources["backgroundMusic"]).Stop();
if (Lala == true)
{
((MediaElement)App.Current.Resources["backgroundMusic"]).Play();
}
Why won't it loop?
Or is there any other wa开发者_如何学Goy to make my BGM loop?
This is what I wrote in the App.xaml and it works, but not sure how to loop it:
<Application.Resources>
<MediaElement x:Name="backgroundMusic" Source="Nyan.mp3" AutoPlay="True" Volume="1" />
</Application.Resources>
Since it's not been fully answered --
you're looking for the MediaElement
to loop itself when it stops its cycle. Your code is not doing that. Instead your code is 1) Stopping the music when the button is clicked. The next line of code: if (Lala == true)
is a bool on a media element.
The simple way to autoloop, even if you have the code entirely in XAML alone, is to insert the MediaEnded=""
code. With this, you create an event handler to specify what to do when the media has ended. So in your example, your XAML will look like this:
<MediaElement x:Name="backgroundMusic" Source="Nyan.mp3" AutoPlay="True" Volume="1" MediaEnded="DoThisWhenMediaEnds" />
and your c# will look like this:
private void DoThisWhenMediaEnds(object sender, RoutedEventArgs e)
{
//what to do when the media has ended. In this case:
backgroundMusic.Play();
}
There you go! You will have all the Nyan goodness you can manage.
I think a better way could be in specifying the property of media element ... Loop="true"
精彩评论