WPF播放和停止多个MediaElements

时间:2011-10-21 11:14:54

标签: wpf loops mediaelement repeat

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

以下是一些代码:

*。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;
        }
    }

1 个答案:

答案 0 :(得分:0)

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

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