MediaElement 在多次播放后失败

发布于 2024-08-17 16:09:37 字数 2263 浏览 6 评论 0原文

我的 MediaElement 控件有问题。我在表单上放置了六个 MediaElement,然后启动它们并通过计时器更改播放的文件。 几次之后,这些元素就会停止播放。

下面是示例 XAML:

<Grid>
<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<MediaElement x:Name="element1" UnloadedBehavior="Close" LoadedBehavior="Manual" Grid.Column="0" Grid.Row="0" />
<MediaElement x:Name="element2" UnloadedBehavior="Close" LoadedBehavior="Manual" Grid.Column="1" Grid.Row="0" />
<MediaElement x:Name="element3" UnloadedBehavior="Close" LoadedBehavior="Manual" Grid.Column="2" Grid.Row="0" />
<MediaElement x:Name="element4" UnloadedBehavior="Close" LoadedBehavior="Manual" Grid.Column="0" Grid.Row="1" />
<MediaElement x:Name="element5" UnloadedBehavior="Close" LoadedBehavior="Manual" Grid.Column="1" Grid.Row="1" />
<MediaElement x:Name="element6" UnloadedBehavior="Close" LoadedBehavior="Manual" Grid.Column="2" Grid.Row="1" />

下面是示例代码:

// 下面的代码对每个 MediaElement 重复

List<string> playlist1 = new List<string>()
{
    @"file1.wmv",
    @"file2.wmv",
    @"file3.wmv",
    @"file4.wmv"
};
DispatcherTimer timer1 = null;
int index1 = 0;
...
void Window1_Loaded(object sender, RoutedEventArgs e)
{
    timer1 = new DispatcherTimer();
    timer1.Tick += new EventHandler(timer1_Elapsed);
    timer1.Interval = TimeSpan.FromSeconds(10);
    element1.Source = new Uri(playlist1[index1]);
    timer1.Start();
    element1.Play();
    ...
}
void timer1_Elapsed(object sender, EventArgs e)
{
    Dispatcher.BeginInvoke(DispatcherPriority.Normal, (System.Threading.ThreadStart)delegate()
    {
        element1.Stop();
        element1.Close();
        timer1.Stop();
        index1++;
        if (index1 >= playlist1.Count)
        {
            index1 = 0;
        }
        element1.Source = new Uri(playlist1[index1]);
        timer1.Start();
        element1.Play();
    });
}

...

有人有类似的问题吗?

I have a problem with MediaElement control. I've put six MediaElements on my form, then I start them and change played files by timer.
After several times, these elements stop playing.

Here is the sample XAML:

<Grid>
<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<MediaElement x:Name="element1" UnloadedBehavior="Close" LoadedBehavior="Manual" Grid.Column="0" Grid.Row="0" />
<MediaElement x:Name="element2" UnloadedBehavior="Close" LoadedBehavior="Manual" Grid.Column="1" Grid.Row="0" />
<MediaElement x:Name="element3" UnloadedBehavior="Close" LoadedBehavior="Manual" Grid.Column="2" Grid.Row="0" />
<MediaElement x:Name="element4" UnloadedBehavior="Close" LoadedBehavior="Manual" Grid.Column="0" Grid.Row="1" />
<MediaElement x:Name="element5" UnloadedBehavior="Close" LoadedBehavior="Manual" Grid.Column="1" Grid.Row="1" />
<MediaElement x:Name="element6" UnloadedBehavior="Close" LoadedBehavior="Manual" Grid.Column="2" Grid.Row="1" />

Here is the sample code:

// The code below repeats for each MediaElement

List<string> playlist1 = new List<string>()
{
    @"file1.wmv",
    @"file2.wmv",
    @"file3.wmv",
    @"file4.wmv"
};
DispatcherTimer timer1 = null;
int index1 = 0;
...
void Window1_Loaded(object sender, RoutedEventArgs e)
{
    timer1 = new DispatcherTimer();
    timer1.Tick += new EventHandler(timer1_Elapsed);
    timer1.Interval = TimeSpan.FromSeconds(10);
    element1.Source = new Uri(playlist1[index1]);
    timer1.Start();
    element1.Play();
    ...
}
void timer1_Elapsed(object sender, EventArgs e)
{
    Dispatcher.BeginInvoke(DispatcherPriority.Normal, (System.Threading.ThreadStart)delegate()
    {
        element1.Stop();
        element1.Close();
        timer1.Stop();
        index1++;
        if (index1 >= playlist1.Count)
        {
            index1 = 0;
        }
        element1.Source = new Uri(playlist1[index1]);
        timer1.Start();
        element1.Play();
    });
}

...

Does anybody have similar problems?

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

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

发布评论

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

评论(1

逐鹿 2024-08-24 16:09:37

我也遇到了类似的问题,发现解决这个问题的唯一方法是动态创建媒体元素。这是代码,我在 XAML 中有一个名为 mePlayClick 的 MediaElement,但您可能不需要或不想要它。

    private void Play_MediaSound()
    {
        // Create new media element dynamically
        MediaElement mediaElement = new MediaElement();

        // Reuse settings in XAML 
        mediaElement.Volume = mePlayClick.Volume;
        mediaElement.Source = mePlayClick.Source;
        mediaElement.AutoPlay = mePlayClick.AutoPlay;

        // WHen the media ends, remove the media element
        mediaElement.MediaEnded += (sender, args) =>
        {
            LayoutRoot.Children.Remove(mediaElement);
            mediaElement = null;
        };

        // Add the media element, must be in visual ui tree
        LayoutRoot.Children.Add(mediaElement);

        // When opened, play
        mediaElement.MediaOpened += (sender, args) =>
        {
            mediaElement.Play();
        };
    }

I had a similar problem too, found the only way around it was to create the media element dynamically. Heres the code, I had a MediaElement in the XAML called mePlayClick but you may not need or want that.

    private void Play_MediaSound()
    {
        // Create new media element dynamically
        MediaElement mediaElement = new MediaElement();

        // Reuse settings in XAML 
        mediaElement.Volume = mePlayClick.Volume;
        mediaElement.Source = mePlayClick.Source;
        mediaElement.AutoPlay = mePlayClick.AutoPlay;

        // WHen the media ends, remove the media element
        mediaElement.MediaEnded += (sender, args) =>
        {
            LayoutRoot.Children.Remove(mediaElement);
            mediaElement = null;
        };

        // Add the media element, must be in visual ui tree
        LayoutRoot.Children.Add(mediaElement);

        // When opened, play
        mediaElement.MediaOpened += (sender, args) =>
        {
            mediaElement.Play();
        };
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文