2017-10-07 127 views
0

我的問題相當簡單,很容易重現。用WPF媒體元素一個接一個地播放2個時間線

我試圖在開始播放5秒鐘的第一首歌曲,並在6秒鐘後通過使用MediaTimeLine和故事板播放第二首歌曲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)); 
     } 
    } 
} 

你會發現第一首歌曲將無法播放,第二個會玩。 如果您評論第二首歌曲 - 第一首歌曲將播放。

如何解決這個問題,所以兩個將根據示例代碼中已經設置的開始時間和持續時間播放?

回答

0

媒體元素似乎不可能處理兩個媒體時間線。

我不得不添加,我不喜歡其他媒體元素..但是這是我的最終解決這個問題..

<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)); 
    } 
} 
} 
相關問題