更改 MediaElement 中的源 (SILVERLIGHT)
我想将 MediaElent 上的歌曲从 track1.mp3 更改为 track2.mp3。这是代码:
MyMediaElement.Stop();
Uri u = new Uri("track2.mp3", UriKind.Relative);
MyMediaElement.Source=u;
MyMediaElement.Play();
MediaElement 更改源但无法启动。这段代码可能有什么问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试将 MyMediaElement.AutoPlay 设置为 true,只要源更改它就应该播放。您还可以使用 MediaElement.SetSource() 方法进行研究,该方法采用流而不是 uri。
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.
我也有同样的问题。我可以在 XAML 中设置自动播放和源代码,它会起作用,但如果我更改代码中的源代码,它不会执行任何操作。
我捕获了控件的 MediaOpened 事件。
问题是它在您更改源后立即点击 Play() ,因此当前状态已关闭。改变状态需要几个时钟周期。所以,如果你把 Play();在该事件处理程序中它将起作用。
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.
你看起来并没有做错什么。以下是我会尝试的一些诊断:-
指定 track2 作为初始文件是否有效?
附加到 MediaFailed 事件,该事件会被解雇吗?
将 TextBlock 绑定到 CurrentState 属性以观察 CurrentState 如何变化。
You don't appear to be doing anything wrong. Here are a couple of diagnostics I would try:-
Specify track2 as the initial file does that work?
Attach to the MediaFailed event, does that get fired?
Bind a TextBlock to the CurrentState property to observe how the CurrentState has changed.
您将
AutoPlay
设置为false
,然后在 MediaOpened 处理程序中执行Play()
。人们在这里暗示了解决方案,但不是很准确。您还可以处理 MediaFailed 事件,以防媒体因某种原因无法打开(找不到文件等)。You set
AutoPlay
tofalse
and then in the MediaOpened handler you doPlay()
. People kind of hinted towards the solution here but not very exact. You could also handle the MediaFailed event just in case the media fails to open for some reason (can't find file etc).