开发者

Play User's Songs using Silverlight

开发者 https://www.devze.com 2023-01-27 18:14 出处:网络
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:

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 bro开发者_如何转开发wser 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.


  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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号