1
Answer

Loop Media Problem (With StoryBoards)

ertan

ertan

13y
1.4k
1

Hi everyone,

I have some trouble. I made transaction between two medias. But it works just one time.. when form load I call play1 method. And after when transaction of second media finished,i calll again play1 but in this time,it doesnt work.I think problem is second or first storyboard.. Can u help me about this ??

and also MediaElementEnded event doesnt work when first media finished.Something is wrong..

Thank you

 

 c# code

------------------------------------- 


using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; using System.Windows.Media.Animation; using System.Threading;  namespace WpfApplication1 {  public partial class MainWindow : Window  {   private Storyboard myStoryboard;   Storyboard board2;   MediaElement mediaElement1;   MediaElement mediaElement2;   DoubleAnimation myDoubleAnimation;   MediaTimeline mediaTime1;   MediaTimeline mediaTime2;   Clock storyboardClock = null;   bool IsMediaOpened = false;   bool IsMedia2Opened = false;    public MainWindow()   {    InitializeComponent();     var myPanel = mypanel;    //StackPanel myPanel = new StackPanel();    // myPanel.Margin = new Thickness(10);    mediaTime1 = new MediaTimeline(new Uri("c:\\test\\video2.mp4"));    mediaTime2 = new MediaTimeline(new Uri("c:\\test\\video1.mp4"));      NameScope.SetNameScope(this, new NameScope());     mediaElement1 = new MediaElement();    mediaElement2 = new MediaElement();       mediaElement1.Name = "mediaElement1";    mediaElement2.Name = "mediaElement2";    mediaElement2.ScrubbingEnabled = true;    mediaElement1.ScrubbingEnabled = true;    mediaElement1.LoadedBehavior = MediaState.Manual;    mediaElement2.LoadedBehavior = MediaState.Manual;     mediaElement1.MediaOpened += new RoutedEventHandler(media_MediaOpened);    mediaElement2.MediaOpened += new RoutedEventHandler(media2_MediaOpened);    this.RegisterName(mediaElement1.Name, mediaElement1);    this.RegisterName(mediaElement2.Name, mediaElement2);     this.RegisterName("video1", mediaElement1);    this.RegisterName("video2", mediaElement2);    myDoubleAnimation = new DoubleAnimation();    myDoubleAnimation.From = 1.0;    myDoubleAnimation.To = 0.0;    myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1));         myStoryboard = new Storyboard();    myStoryboard.CurrentTimeInvalidated += new EventHandler(storyboard_CurrentTimeInvalidated);    myStoryboard.Children.Add(mediaTime1);     board2 = new Storyboard();    board2.CurrentTimeInvalidated += new EventHandler(storyboard2_CurrentTimeInvalidated);    board2.Children.Add(mediaTime2);    mediaElement2.LoadedBehavior = MediaState.Play;           mediaElement1.MediaEnded += new RoutedEventHandler(MediaElementEnded);    mediaElement1.Loaded += new RoutedEventHandler(MediaElementLoaded);    myDoubleAnimation.Completed+=new EventHandler(myDoubleAnimation_Completed);    mediaElement2.MediaEnded += new RoutedEventHandler(MediaElement2Ended);    myPanel.Children.Add(mediaElement1);    myPanel.Children.Add(mediaElement2);         }    private void media_MediaOpened(object sender, RoutedEventArgs e)   {     this.IsMediaOpened = true;   }     private void media2_MediaOpened(object sender, RoutedEventArgs e)   {       this.IsMedia2Opened = true;   }   private void MediaElementEnded(object sender, RoutedEventArgs e)   {        // play1();    //mediaElement1.BeginAnimation(MediaElement.OpacityProperty,myDoubleAnimation);   }    private void MediaElement2Ended(object sender, RoutedEventArgs e)   {   // play1();    // mediaElement2.BeginAnimation(MediaElement.OpacityProperty, myDoubleAnimation);   }     private void MediaElementLoaded(object sender, RoutedEventArgs e)   {    play1();   }     void play1()    {    board2.Stop(mediaElement2);    myStoryboard.Stop(mediaElement1);    myStoryboard.Remove(this);    board2.Remove(this);     Storyboard.SetTargetName(mediaTime1, "video1");    Storyboard.SetTargetName(myDoubleAnimation, "video1");    myStoryboard.Begin(mediaElement1);         }      void play2()    {    // board2.Children.Add(mediaTime2);       Storyboard.SetTargetName(mediaTime2, "video2");    Storyboard.SetTargetName(myDoubleAnimation, "video2");    board2.Begin(mediaElement2);    }   bool workOneTime = true;   private void storyboard_CurrentTimeInvalidated(object sender, EventArgs e)   {    storyboardClock = (Clock)sender;    if (IsMediaOpened && storyboardClock.CurrentTime != null && workOneTime)    {          if ( storyboardClock.CurrentTime.Value.Seconds>=mediaElement1.NaturalDuration.TimeSpan.Seconds - 1)     {                workOneTime = false;                mediaElement1.BeginAnimation(MediaElement.OpacityProperty, myDoubleAnimation);            play2();           }    }   }    bool workOneTime2= true;    private void storyboard2_CurrentTimeInvalidated(object sender, EventArgs e)   {      storyboardClock = (Clock)sender;     if (IsMedia2Opened && storyboardClock.CurrentTime != null && workOneTime2)     {            if (storyboardClock.CurrentTime.Value.Seconds >= mediaElement2.NaturalDuration.TimeSpan.Seconds- 0.5)      {             workOneTime2 = false;       mediaElement2.BeginAnimation(MediaElement.OpacityProperty, myDoubleAnimation);       play1();      }      }   }    public void myDoubleAnimation_Completed(object sender, EventArgs e) {     myStoryboard.FillBehavior = FillBehavior.Stop;    board2.FillBehavior = FillBehavior.Stop;    mediaElement1.Stop();    mediaElement1.Close();        if(mediaElement2.Position == mediaElement2.NaturalDuration.TimeSpan)    mediaElement2.Stop();     myStoryboard.Stop();    board2.Stop();          }  } } 


 



 

-----------------------------------

xaml

 

 

<Window x:Class="WpfApplication1.MainWindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="MainWindow" Height="350" Width="525" Background="White" >   <Grid Background="Green">     <Grid Name="mypanel">   </Grid>        </Grid> </Window>  

Answers (1)