Silverlight 4 MediaElement播放声音
我将本地声音文件转换为资源,该资源在我的 XAML 中构建了此文件:
<UserControl.Resources>
<my:Uri x:Key="SoundFiles">file:///c:/Audio/HebrewDemo/Shalom.wav</my:Uri>
</UserControl.Resources>
我通过将本地磁盘 mp3 文件名粘贴到源中来完成此操作,然后单击源的“点”并选择“提取值到资源”。
当我运行时,它告诉我“Uri”无效,果然,在智能感知中,我看到其他以“uri”开头的元素,而不仅仅是 URI 本身。
在现实世界中,我想指定一个动态 mp3 文件名。例如,我可能有一个用于抽认卡的外语单词数据库,我想在 URL 上播放声音文件。但我想我应该在跑步之前尝试走路...
现在我正在尝试这样做:
mediaElement1.Source = new Uri(
"http://HebrewResources.com/SoundFiles/Shalom.mp3",
UriKind.Absolute);
mediaElement1.Play();
FireFox 浏览器中的状态栏指示正在从网站传输一些数据。然而,我从来没有听到任何声音。难道只是编码问题?如果编码不正确,我会收到错误吗?
另外,我可以将 Uri 语句放在加载中或使其在后台运行,以便用户可以在下载声音文件的同时阅读屏幕吗?换句话说,当他单击按钮来收听声音文件时,理想情况下该声音文件已经为他预加载了。在这个语言学习应用程序中,用户将看到一个外语单词,并尝试自己发音,然后他将单击“播放”按钮来听声音以检查他的结果。
I converted a local sound file to a resource, which built this in my XAML:
<UserControl.Resources>
<my:Uri x:Key="SoundFiles">file:///c:/Audio/HebrewDemo/Shalom.wav</my:Uri>
</UserControl.Resources>
I did this by pasting a local disk mp3 filename into source, then clicked on the "dot" by source and chose "Extract Value to Resource".
When I run, it tells me that "Uri" is not valid, and sure enough, in the Intellisense, I see other elements that start with "uri" but not just URI by itself.
In the real world I want to specify a dynamic mp3 file name. For example, I might have a database of foreign language words used for flashcards, I want to play a sound file on a URL. But I thought I would try to walk before running...
Now I'm trying this:
mediaElement1.Source = new Uri(
"http://HebrewResources.com/SoundFiles/Shalom.mp3",
UriKind.Absolute);
mediaElement1.Play();
The status bar in the FireFox browser indicates some data being transferred from the website. However, I never hear any sound. Could it just be an encoding issue? If it is not encoded properly, would I get an error?
Also, can I put the Uri statement in the load or make it run in the background, so the user can read the screen, at the same time the sound file is downloading? In other words, when he clicks the button to hear the soundfile, ideally it would already be preloaded for him. In this language-learning app, the user will see a word in a foreign language, and try to pronounce it himself, then he will click the "Play" button to hear the sound to check his results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 Silverlights“沙箱”安全模型,指向本地文件的第一个代码在普通 Silverlight 应用程序中无法工作。在浏览器中运行普通的 Silverlight 应用程序时,您无法像运行已安装的 winforms/WPF 应用程序那样访问本地资源。观看此视频教程 http://www.silverlight。 net/learn/videos/all/local-file-access/,如果您想了解有关使用 Silverlight 访问本地文件的更多信息。
关于您的第二段代码,它应该可以工作,因此很可能是编码问题,不幸的是您通常不会遇到此类错误。
我创建了一个示例应用程序并将其指向您的 mp3 文件,但它不起作用,但是快速搜索示例 mp3,将我带到另一个免费可用的(第一次点击)。使用下面的代码/url,它工作正常。
关于加载,当它到达设置媒体元素源的代码时,它将开始下载文件。因此,如果您在构造函数或加载事件中设置媒体元素的源,它将自动在后台开始下载。然后你只需调用 myMediaelement.Play();在按钮单击事件上。
如果您发现使用 Firefox 在确定加载外部文件或 Web 服务等内容以及何时加载时有点受限。有一个很棒的免费工具,称为 Fiddler (http://www.fiddlertool.com/fiddler/version。 asp),这将使您可以非常轻松地监视这些事情。
祝你好运 :)
The first code which points to the local file won't work in a normal Silverlight application because of Silverlights "sanboxed" security model. Running a normal Silverlight application in your browser, you can't access local resources like you can if you were running an installed winforms/WPF application. Have a look at this video tutorial http://www.silverlight.net/learn/videos/all/local-file-access/, if you want to learn more about accessing local files using Silverlight.
In regards to your second piece of code, it should work, so quite possibly it's an encoding issue, and no unfortunately you often don't get any errors for things like that.
I created a sample app and pointed it at your mp3 file and it' wouldn't work, however a quick search for sample mp3s, lead me to another freely available (first hit). Using the code/url below, it works ok.
In regards to the loading, it will start downloading the file when it hits the code that sets the source of the mediaelement. So if you set the source for the media element in your constructor or in your loaded event, it will automatically start downloading in the background. Then you just call myMediaelement.Play(); on the button click event.
If you find using Firefox a bit limited, when it comes to determine what and when something like external files or webservices are being loaded. There's a great free tool called Fiddler (http://www.fiddlertool.com/fiddler/version.asp) which will let you monitor those things very easily.
Good luck :)