Skip to main content

Microsoft Silverlight

Answered Question Switching Source of MediaElement at Runtime (Silverlight 2.0)RSS Feed

(0)

quilnux
quilnux

Member

Member

26 points

44 Posts

Switching Source of MediaElement at Runtime (Silverlight 2.0)

I have a project which I am trying to play 3 WMV videos, each video is 5 minutes long.

 I can play the first video just fine but when it's finished I use the MediaEnded event handler to switch the source for the second video.

Uri VideoUri = new Uri("http://localhost/Videos/Video-2.wmv"); 

MediaElement.Source = VideoUri;

But when I do that the connection closes. If I load a video that is 10 seconds or less and after playing that, try to load the second video it works. It just won't if the video is longer then 10 seconds.

I think it may be due to the browser connection being closed by KeepAlive after 15 seconds of idle.

Is there a way to reopen the browser connection in Silverlight or is there another way to do what I am wanting?

 Thanks.

fullsailrick
fullsail...

Contributor

Contributor

3699 points

829 Posts

Answered Question

Re: Switching Source of MediaElement at Runtime (Silverlight 2.0)

Hi! Are you setting this new video like this?

myPlayer.Stop();
myPlayer.Source = new Uri("http://localhost/Videos/GirlsGoneWild4.wmv", UriKind.Absolute);
myPlayer.Position = TimeSpan.FromSeconds(0);
myPlayer.Play();

quilnux
quilnux

Member

Member

26 points

44 Posts

Re: Switching Source of MediaElement at Runtime (Silverlight 2.0)

Basically, We don't set the position to zero though, You have:

myPlayer.Position = TimeSpan.FromSeconds(0);

I was assuming that it resets the position to 0 when you switch the source. Do I assume wrong?

 Thanks,

fullsailrick
fullsail...

Contributor

Contributor

3699 points

829 Posts

Re: Switching Source of MediaElement at Runtime (Silverlight 2.0)

Oh, I have no idea. lol I've just always set it. Do you still have the problem?

struggle-luan
struggle...

Member

Member

578 points

102 Posts

Re: Switching Source of MediaElement at Runtime (Silverlight 2.0)

 ...Ahhh when you re-set the source, mediaelement will reload all data...

If you want to "Continue" your position, you may face to the streaming problem, that is, your media data will be streamed from 0, you want to watch it at "70" which is just the previous media's "current position".

I think a better way to solve this is create 3 mediaelment (which is 3 parts of your "whole" clip), and you have to add them to your visual-tree otherwise they won't load!

And you've to set 2 Mediaelement's postion to your target position or just let them buffering, and also Opacity = 0, not visiblity... And volume = 0; 

Ok when your 1st media is "at the point", you can dispose it and let "2nd" shows up....

 

Interesting condition uh?

good luck~

 

ehh... this is what I called "POOR ENGLISH"..

quilnux
quilnux

Member

Member

26 points

44 Posts

Re: Switching Source of MediaElement at Runtime (Silverlight 2.0)

struggle-luan:

 ...Ahhh when you re-set the source, mediaelement will reload all data...

If you want to "Continue" your position, you may face to the streaming problem, that is, your media data will be streamed from 0, you want to watch it at "70" which is just the previous media's "current position".

I think a better way to solve this is create 3 mediaelment (which is 3 parts of your "whole" clip), and you have to add them to your visual-tree otherwise they won't load!

And you've to set 2 Mediaelement's postion to your target position or just let them buffering, and also Opacity = 0, not visiblity... And volume = 0; 

Ok when your 1st media is "at the point", you can dispose it and let "2nd" shows up....

 

Interesting condition uh?

good luck~

 

We wanted to do that but we found out that the 3 clips are hosted on 3 different servers and we are not able to put them together. This was our original design plan.

quilnux
quilnux

Member

Member

26 points

44 Posts

Re: Switching Source of MediaElement at Runtime (Silverlight 2.0)

fullsailrick:
Oh, I have no idea. lol I've just always set it. Do you still have the problem?

Checking now. I just added the TimeSpan and initial testing was promising but I'm trying against the published video files so I will know in about 3 minutes 12 seconds so I'll let you know.

quilnux
quilnux

Member

Member

26 points

44 Posts

Re: Switching Source of MediaElement at Runtime (Silverlight 2.0)

fullsailrick:
Oh, I have no idea. lol I've just always set it. Do you still have the problem?

heh, wow.. Adding the TimeSpan.FromSeconds(0); fixed it.

Thanks for the help! It's very interesting.

YoungOZ
YoungOZ

Member

Member

16 points

4 Posts

Re: Re: Switching Source of MediaElement at Runtime (Silverlight 2.0)

Can you please post the codes that you are currently using to play the Video. I'm trying to accomplish the same task.

 

Thanks

Sincerely;Oscar Martinez

quilnux
quilnux

Member

Member

26 points

44 Posts

Re: Re: Switching Source of MediaElement at Runtime (Silverlight 2.0)

YoungOZ:

Can you please post the codes that you are currently using to play the Video. I'm trying to accomplish the same task.

 

Thanks

 

private void VideoPlayer_MediaEnded(object sender, RoutedEventArgs e)

{

VideoPlayer.AutoPlay = true;if (VideoIndex == 0)

{

Uri VideoUri =
new Uri([VideoUrl]);

VideoPlayer.Source = VideoUri;

VideoPlayer.Position =
TimeSpan.FromSeconds(0);

VideoIndex = 1;

}

else if (VideoIndex == 1)

{

Uri VideoUri =
new Uri([VideoUrl]);

VideoPlayer.Source = VideoUri;

VideoPlayer.Position =
TimeSpan.FromSeconds(0);

VideoIndex = 2;

}

else if (VideoIndex == 2)

{

VideoPlayer.AutoPlay =
false;Uri VideoUri = new Uri([VideoUrl]);

VideoPlayer.Source = VideoUri;

VideoPlayer.Position =
TimeSpan.FromSeconds(0);

VideoIndex = 0;

}

}

 

VideoIndex is a variable that is used to determine which video is being played. I assign each video an index and use that index to track which is playing so I know what is next in the playlist. You will want to add some boolean or something for your stop button if you have one because the stop button still cause this code to execute under MediaEnded. I just put all this code around an if conditional and had the stop button change a boolean to true. if the boolean is true, the stop button was pressed, don't execute this code. If it's false and the media ended (got to the end of the video itself) then move to the next video. If the last video was played, reset the video to the first one and reset the VideoIndex to 0.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities