簡體   English   中英

使用 WPF 媒體元素依次播放 2 個時間線

[英]Playing 2 timelines one after another with WPF media-element

我的問題相當簡單,而且很容易重現。

我正在嘗試使用MediaTimeLine和 storyboard 在開始時播放第一首歌曲 5 秒,在 6 秒后播放第二首歌曲 10 秒。

這是代碼,只需打開一個新的 WPF 應用程序並復制 XAML 和代碼即可重現它。 哦,並確保更改您在機器中本地獲取的 2 首歌曲的路徑。

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <MediaElement Name="MediaElementObject1" />
    <Button Width="100" Height="100" Margin="26,48,391,171" Click="Button_Click"></Button>
</Grid>

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private readonly Storyboard animation = new Storyboard();

        public MainWindow()
        {
            InitializeComponent();

            var mediaUri1 = new Uri(@"C:\MediaExamplesForWork\Sting - Sting At The Movies - 07 - Shape OF My Heart (Leon & Three Of Hearts).mp3");
            var mediaUri2 = new Uri(@"C:\MediaExamplesForWork\Desireless - Voyage Voyage.mp3");
            animation.Duration = TimeSpan.FromSeconds(20);

            var timeline1 = new MediaTimeline
            {
                Name = "a1",
                BeginTime = TimeSpan.Zero, 
                Duration = TimeSpan.FromSeconds(5),
                Source = mediaUri1,
                FillBehavior = FillBehavior.HoldEnd
            };
            Storyboard.SetTarget(timeline1, MediaElementObject1);
            animation.Children.Add(timeline1);

            var timeline2 = new MediaTimeline
            {
                Name = "a2",
                BeginTime = TimeSpan.FromSeconds(6),
                Duration = TimeSpan.FromSeconds(10),
                Source = mediaUri2,
                FillBehavior = FillBehavior.HoldEnd
            };
            Storyboard.SetTarget(timeline2, MediaElementObject1);
            animation.Children.Add(timeline2);

            animation.Begin();    
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            animation.SeekAlignedToLastTick(TimeSpan.FromSeconds(2));
        }
    }
}

您會注意到第一首歌曲不會播放,第二首會播放。 如果您評論第二首歌曲 - 第一首將播放。

如何修復它以便兩者都根據示例代碼中已經設置的開始時間和持續時間播放?

媒體元素似乎無法處理兩條媒體時間線。

我不得不添加另一個我不喜歡的媒體元素。但這就是我最終解決此問題的方式。

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <MediaElement Name="MediaElementObject1" />
    <MediaElement Name="MediaElementObject2" />
    <Button Width="100" Height="100" Margin="26,48,391,171" Click="Button_Click"></Button>
</Grid>

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    private readonly Storyboard animation = new Storyboard();

    public MainWindow()
    {
        InitializeComponent();

        var mediaUri1 = new Uri(@"C:\MediaExamplesForWork\Sting - Sting At The Movies - 07 - Shape OF My Heart (Leon & Three Of Hearts).mp3");
        var mediaUri2 = new Uri(@"C:\MediaExamplesForWork\Desireless - Voyage Voyage.mp3");

        animation.Duration = TimeSpan.FromSeconds(20);


        var timeline1 = new MediaTimeline
        {
            Name = "a1",
            BeginTime = TimeSpan.Zero, 
            Duration = TimeSpan.FromSeconds(5),
            Source = mediaUri1,
            FillBehavior = FillBehavior.HoldEnd
        };
        Storyboard.SetTarget(timeline1, MediaElementObject1);
        animation.Children.Add(timeline1);


        var timeline2 = new MediaTimeline
        {
            Name = "a2",
            BeginTime = TimeSpan.FromSeconds(6),
            Duration = TimeSpan.FromSeconds(10),
            Source = mediaUri2,
            FillBehavior = FillBehavior.HoldEnd
        };
        Storyboard.SetTarget(timeline2, MediaElementObject2);
        animation.Children.Add(timeline2);

        animation.Begin();

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        animation.SeekAlignedToLastTick(TimeSpan.FromSeconds(13));
    }
}
}

據我所知,TimeLine 的 BeginTime 屬性不會影響媒體文件的開始時間。 媒體文件將始終從 position 0 開始。當 BeginTime=6 且 Duration=10 時,您將看到 6 秒無內容,然后視頻將從 BEGINNING 開始播放 10 秒。 使用 MediaTimeline 似乎不是指定視頻剪輯開始時間的有效方法。 希望有人能證明我錯了。

另一種方法是設置 Position 屬性,然后調用 Play,然后使用計時器監視何時到達結束時間。 看起來有點笨拙,但它確實有效。 我還沒有看到更好的方法。 然后您也可以用同樣的方式開始第二個剪輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM