几次播放后,MediaElement失败

时间:2010-01-14 14:32:56

标签: c# wpf video mediaelement

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

以下是示例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" />

以下是示例代码:

// 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();
    });
}

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(); }); }

有没有人有类似的问题?

1 个答案:

答案 0 :(得分:2)

我也有类似的问题,发现唯一的方法是动态创建媒体元素。下面是代码,我在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();
        };
    }