更改 MediaElement 中的源 (SILVERLIGHT)

发布于 2024-08-21 09:56:47 字数 249 浏览 6 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三岁铭 2024-08-28 09:56:48

尝试将 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.

念﹏祤嫣 2024-08-28 09:56:48

我也有同样的问题。我可以在 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.

2024-08-28 09:56:48

你看起来并没有做错什么。以下是我会尝试的一些诊断:-

指定 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.

酷炫老祖宗 2024-08-28 09:56:48

您将 AutoPlay 设置为 false,然后在 MediaOpened 处理程序中执行 Play()。人们在这里暗示了解决方案,但不是很准确。您还可以处理 MediaFailed 事件,以防媒体因某种原因无法打开(找不到文件等)。

You set AutoPlay to false and then in the MediaOpened handler you do Play(). 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文