使用 Silverlight 播放用户的歌曲

发布于 2024-10-05 04:19:03 字数 571 浏览 3 评论 0原文

有谁有一个有效的(经过测试的)代码示例来从独立存储中播放音频文件。我目前拥有的代码(不会引发异常或发出任何声音)是:

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

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

发布评论

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

评论(1

写给空气的情书 2024-10-12 04:19:03
  1. 您不能使用与 SetSource 命令相同的方法来使用 Play 命令,因为文件将异步打开。通过将自动播放设置为 true(这也是默认设置)。您确保加载后即可播放。

    MediaElement ME = new MediaElement();
    ME.自动播放 = true;
    isolatedStorageFile ISF =isolatedStorageFile.GetUserStoreForApplication();
    ME.SetSource(ISF.OpenFile("foo.wma", FileMode.Open));
    
  2. Silvelright 本身不支持 wav 文件,因此要播放 wav 文件,您需要下载 http:// code.msdn.microsoft.com/wavmss,然后使用以下代码。

    MediaElement ME = new MediaElement();
    ME.自动播放 = true;
    isolatedStorageFile ISF =isolatedStorageFile.GetUserStoreForApplication();
    ME.SetSource(new WaveMediaStreamSource(ISF.OpenFile("foo.wav", FileMode.Open)));
    

    尽管并不理想,但您可以使用文件扩展名来检测何时播放 wav 文件,并仅在这种情况下使用第二个代码示例。

  1. 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.

    MediaElement ME = new MediaElement();
    ME.AutoPlay = true;
    IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
    ME.SetSource(ISF.OpenFile("foo.wma", FileMode.Open));
    
  2. 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.

    MediaElement ME = new MediaElement();
    ME.AutoPlay = true;
    IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication();
    ME.SetSource(new WaveMediaStreamSource(ISF.OpenFile("foo.wav", FileMode.Open)));
    

    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.

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