如何在WP7 BGM中循环播放音乐?

发布于 2024-11-09 15:05:13 字数 568 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳絮泡泡 2024-11-16 15:05:13

由于它尚未得到完全解答 -

您正在寻找 MediaElement 在停止循环时自行循环。你的代码没有这样做。相反,您的代码是 1)单击按钮时停止音乐。下一行代码:if (Lala == true) 是媒体元素上的 bool。

即使代码完全在 XAML 中,自动循环的简单方法也是插入 MediaEnded="" 代码。这样,您可以创建一个事件处理程序来指定媒体结束时要执行的操作。因此,在您的示例中,您的 XAML 将如下所示:

 <MediaElement x:Name="backgroundMusic" Source="Nyan.mp3" AutoPlay="True" Volume="1" MediaEnded="DoThisWhenMediaEnds" />

您的 c# 将如下所示:

 private void DoThisWhenMediaEnds(object sender, RoutedEventArgs e)
 {
    //what to do when the media has ended. In this case:
    backgroundMusic.Play();
 }

好了!你将拥有你所能管理的所有 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:

 <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.

乱世争霸 2024-11-16 15:05:13

我认为更好的方法可能是指定媒体元素的属性... Loop="true"

I think a better way could be in specifying the property of media element ... Loop="true"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文