使用 Silverlight 播放用户的歌曲
有谁有一个有效的(经过测试的)代码示例来从独立存储中播放音频文件。我目前拥有的代码(不会引发异常或发出任何声音)是:
MediaElement ME = new MediaElement();
ME.AutoPlay = false;
IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
ME.SetSource(ISF.OpenFile("foo.wav", FileMode.Open));
ME.Play();
我已经尝试使用多种不同的音频格式(使用表达式进行编码),但我总是遇到同样的问题。
另外,我非常喜欢一个使用文件浏览器从文件流加载歌曲的示例,但这并不重要,并且可以轻松转换独立存储示例。
我已经检查过,如果我将文件嵌入到应用程序中,它就可以正常运行。问题是我希望用户能够将自己的歌曲加载到应用程序中,然后将其存储在独立存储中并从独立存储中播放。
最后,与示例一样,我宁愿在 C# 代码中执行此操作,而不是 XAML。
Does anyone have a working (tested) example of code to play an audio file from isolated storage. The code I currently have, which doesn't throw an exception or make any sound, is:
MediaElement ME = new MediaElement();
ME.AutoPlay = false;
IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
ME.SetSource(ISF.OpenFile("foo.wav", FileMode.Open));
ME.Play();
I've tried this using a number of different audio formats, encoded using Expression, but I always have the same problem.
Also, I'd quite like an example using the file browser to load the song from a file stream, however this is less important and the Isolated Storage example could easily be converted.
I've checked, and if I embed the file in the application, it plays fine. The problem is I want users to be able to load their own songs into the application, which will then be stored in and played from isolated storage.
Finally, as with the example, I'd rather be doing this in C# code, rather than XAML.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用与 SetSource 命令相同的方法来使用 Play 命令,因为文件将异步打开。通过将自动播放设置为 true(这也是默认设置)。您确保加载后即可播放。
Silvelright 本身不支持 wav 文件,因此要播放 wav 文件,您需要下载 http:// code.msdn.microsoft.com/wavmss,然后使用以下代码。
尽管并不理想,但您可以使用文件扩展名来检测何时播放 wav 文件,并仅在这种情况下使用第二个代码示例。
You can't have the Play command in the same method as the SetSource command since the file will be opened asynchronously. By setting AutoPlay to true (which is also the defualt). You ensure that it will play as soon as it's loaded.
Silvelright doesn't natively support wav files, so to play wav files you need to download http://code.msdn.microsoft.com/wavmss, then use the following code.
Although not ideal, you can use the file extensions to detect when a wav file is being played and use the second code sample only in this case.