Lately I've been doing some research on how to play music in silverlight application, and this is the situation I face right now:
(1) I can use MediaElement
, but it can only play one music file at a time, due to I couldn't use usercontrols in my project, this may not be the solution.
(2) I can use SoundEffect, but it only supports wav files which are pretty huge even at medium quality.
Since I want to play multiple music files at the same time and control whether to loop it, I think the latter one may be my solution.
I don't put a music file in my project(So the Load<Song>
Function won't work for me), instead, I put them in the database, after I get all the bytes then I can play it, below is the snippet of the codes:
MemoryStream stream = new MemoryStream(bytes);
SoundEffect sound = SoundEffect.FromStream(stream);
SoundEffectInstance soundInstance = sound.CreateInstance();
if (loopValue.Number == 1.0) soundInstance.IsLooped = true;
soundInstance.Play();
This works well with wav file, but if it throws exception when i use it to play mp3 file:
System.Exception: 对 COM 组件的调用返回了错误 HRESULT E_FAIL。
位于 Microsoft.Xna.Framework.Audio.XcpImports.CheckHResult(UInt32 hr)
位于 Microsoft.Xna.Framework.Audio.XcpImports.SoundEffect_LoadWavFromStream(SoundEffect soundEffect, Stream stream)
位于 Microsoft.Xna.Framework.Audio.SoundEffect.FromStream(Stream stream)
....
I've not found any library which can directly convert mp3 to wav in silverlight platform, I am not so aware of the inside of SoundEffect, so is anyone who can tell me how to convert mp3 byte stream to wav, so SoundEffect can works ?
btw, due to performance issue, I wouldn't use wcf service to send bytes, and convert them in the hosted web application, it'll adds much pressure on my server, so library like NAudio may not help.
So, any great soluton to this puzzle, I would appreciate for your help.