3

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;
        }
    }
janus
  • 31
  • 3
  • 1
    Good question.. I just want to know also why 2 video doesn't work at the same time? Only 1 video plays and the second doesn't. But there is a sound of the second video... That's odd! – NoWar Nov 11 '11 at 16:27
  • Peretz, I'm having a very same problem. Each time set the source of one MediaElement, another MediaElement disappears. – Amadeusz Wieczorek Jan 19 '13 at 10:38

1 Answers1

0

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.

Kelly
  • 6,992
  • 12
  • 59
  • 76