如何在WP7 BGM中循环播放音乐?
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();
}
为什么不循环?
或者还有其他方法让我的BGM循环播放吗?
这是我在 App.xaml 中编写的内容,它可以工作,但不确定如何循环它:
<Application.Resources>
<MediaElement x:Name="backgroundMusic" Source="Nyan.mp3" AutoPlay="True" Volume="1" />
</Application.Resources>
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 way 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于它尚未得到完全解答 -
您正在寻找
MediaElement
在停止循环时自行循环。你的代码没有这样做。相反,您的代码是 1)单击按钮时停止音乐。下一行代码:if (Lala == true)
是媒体元素上的 bool。即使代码完全在 XAML 中,自动循环的简单方法也是插入
MediaEnded=""
代码。这样,您可以创建一个事件处理程序来指定媒体结束时要执行的操作。因此,在您的示例中,您的 XAML 将如下所示:您的 c# 将如下所示:
好了!你将拥有你所能管理的所有 Nyan 善良。
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:and your c# will look like this:
There you go! You will have all the Nyan goodness you can manage.
我认为更好的方法可能是指定媒体元素的属性... Loop="true"
I think a better way could be in specifying the property of media element ... Loop="true"