Loop Media Problem (With StoryBoards)

Posted by Ertan2002 under WPF on 6/29/2011 | Points: 10 | Views : 2141 | Status : [Member] | Replies : 1
Hi everyone,

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 ??
Thank you



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



}







Responses

Posted by: Ertan2002 on: 6/29/2011 [Member] Starter | Points: 25

Up
0
Down
continue ...


void play2()
{


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

}
}




Ertan2002, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response