WPF 播放和停止多个 MediaElement
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道使用 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.