Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Source in MediaElement (SILVERLIGHT)

I would like to change song on my MediaElent from track1.mp3 to track2.mp3. And here is the code:

MyMediaElement.Stop();
Uri u = new Uri("track2.mp3", UriKind.Relative);
MyMediaElement.Source=u;
MyMediaElement.Play();

The MediaElement change the source but just won't start. What could possibly be wrong with this code?

like image 287
Krika Avatar asked Feb 01 '26 14:02

Krika


2 Answers

Try setting MyMediaElement.AutoPlay to true, as soon as the source changes it should play. You could also investigate using the MediaElement.SetSource() method which takes a stream rather than a uri.

like image 157
Jason Roberts Avatar answered Feb 04 '26 06:02

Jason Roberts


I had the same problem. I could set autoplay and the source in the XAML and it would work, but if I changed source in the code it would just do nothing.
I captured the MediaOpened event of the control.
The problem is that it hits Play() right after you changed the source, so the current state is closed. It takes a few clock cycles to change the state. So, if you put Play(); inside that event handler it will work.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!