WPF 播放和停止多个 MediaElement

发布于 2024-12-11 08:07:56 字数 2255 浏览 0 评论 0原文

我对 WPF 中的多个 MediaElement 有疑问。我只想一次又一次地播放一个 MediaElement,直到音频索引发生变化。声源只是一个简短的词,所以我会重复/循环它。但问题是,玩了一段时间就停止了,不再玩了。有人可以帮助我吗?

这是一些代码:

*.xaml

<MediaElement x:Name="mediaElementStrawberries" LoadedBehavior="Manual" UnloadedBehavior="Manual" MediaEnded="mediaElementStrawberriesEnded" Source="Media\Strawberries.mp3"/>
<MediaElement x:Name="mediaElementBlackberries" LoadedBehavior="Manual" UnloadedBehavior="Manual" MediaEnded="mediaElementBlackberriesEnded" Source="Media\Blackberries.mp3"/>
<MediaElement x:Name="mediaElementBlueberries" LoadedBehavior="Manual" UnloadedBehavior="Manual" MediaEnded="mediaElementBlueberriesEnded" Source="Media\Blueberries.mp3"/>

*.cs

 if (audioindex == 1)
 {
     mediaElementStrawberries.Play();
 }
 if (audioindex == 2)
 {
     mediaElementBlackberries.Play();
 }
 if (audioindex == 3)
 {
     mediaElementBlueberries.Play();
 }

 public void mediaElementStrawberriesEnded(object sender, RoutedEventArgs e)
 {
        if (audioindex == 1)
        {
            mediaElemenStrawberries.Position = TimeSpan.Zero;
            mediaElementStrawberries.LoadedBehavior = MediaState.Play;
        }
        else if (audioindex == 2)
        {
            mediaElementBlackberries.Position = TimeSpan.Zero;
            mediaElementBlackberries.LoadedBehavior = MediaState.Play;
        }
        else if (audioindex == 3)
        {
            mediaElementBlueberries.Position = TimeSpan.Zero;
            mediaElementBlueberries.LoadedBehavior = MediaState.Play;
        }
    }

    private void mediaElementBlackberriesEnded(object sender, RoutedEventArgs e)
    {
        if (audioindex == 2)
        {
            mediaElementBlackberries.Position = TimeSpan.Zero;
            mediaElementBlackberries.LoadedBehavior = MediaState.Play;
        }
        else if (audioindex == 1)
        {
            mediaElementStrawberries.Position = TimeSpan.Zero;
            mediaElementStrawberries.LoadedBehavior = MediaState.Play;
        }
        else if (audioindex == 3)
        {
            mediaElementBlueberries.Position = TimeSpan.Zero;
            mediaElementBlueberries.LoadedBehavior = MediaState.Play;
        }
    }

I've a question about multiple MediaElement in WPF. I want to play only one MediaElement again and again at the same time, until the audioindex changes. The soundsource is only a short word so i'll repeat/loop it. But the problem is, that it stops after a while without playing anymore. Could anybody help me?

Here is some code:

*.xaml

<MediaElement x:Name="mediaElementStrawberries" LoadedBehavior="Manual" UnloadedBehavior="Manual" MediaEnded="mediaElementStrawberriesEnded" Source="Media\Strawberries.mp3"/>
<MediaElement x:Name="mediaElementBlackberries" LoadedBehavior="Manual" UnloadedBehavior="Manual" MediaEnded="mediaElementBlackberriesEnded" Source="Media\Blackberries.mp3"/>
<MediaElement x:Name="mediaElementBlueberries" LoadedBehavior="Manual" UnloadedBehavior="Manual" MediaEnded="mediaElementBlueberriesEnded" Source="Media\Blueberries.mp3"/>

*.cs

 if (audioindex == 1)
 {
     mediaElementStrawberries.Play();
 }
 if (audioindex == 2)
 {
     mediaElementBlackberries.Play();
 }
 if (audioindex == 3)
 {
     mediaElementBlueberries.Play();
 }

 public void mediaElementStrawberriesEnded(object sender, RoutedEventArgs e)
 {
        if (audioindex == 1)
        {
            mediaElemenStrawberries.Position = TimeSpan.Zero;
            mediaElementStrawberries.LoadedBehavior = MediaState.Play;
        }
        else if (audioindex == 2)
        {
            mediaElementBlackberries.Position = TimeSpan.Zero;
            mediaElementBlackberries.LoadedBehavior = MediaState.Play;
        }
        else if (audioindex == 3)
        {
            mediaElementBlueberries.Position = TimeSpan.Zero;
            mediaElementBlueberries.LoadedBehavior = MediaState.Play;
        }
    }

    private void mediaElementBlackberriesEnded(object sender, RoutedEventArgs e)
    {
        if (audioindex == 2)
        {
            mediaElementBlackberries.Position = TimeSpan.Zero;
            mediaElementBlackberries.LoadedBehavior = MediaState.Play;
        }
        else if (audioindex == 1)
        {
            mediaElementStrawberries.Position = TimeSpan.Zero;
            mediaElementStrawberries.LoadedBehavior = MediaState.Play;
        }
        else if (audioindex == 3)
        {
            mediaElementBlueberries.Position = TimeSpan.Zero;
            mediaElementBlueberries.LoadedBehavior = MediaState.Play;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

落花随流水 2024-12-18 08:07:56

我知道使用 SoundPlayer 和使用 MediaElement 它仅限于单个声道。因此,如果您播放一个声音然后尝试播放第二个声音,第二个声音将切断第一个声音。 (单声道声音)

我假设您有类似的问题,不幸的是我发现解决它的唯一方法是使用 XNA(现已停产)或转到 DirectX。

I know with SoundPlayer and using MediaElement it's limited to a single sound channel. So if you play one sound and then try to play a second the second will cut off the first. (single channel sound)

I assume your having a similar issue, and unfortunately the only way I've found to fix it is using XNA (which is now discontinued) or going to DirectX.

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