WP7 play many compressed (mp3, wma etc) audio files simultaneously/dynamically

强颜欢笑 提交于 2019-11-29 14:39:29
Gergely Orosz

In my experience currently there's no good solution for this on WP7. Either you use wavs with XNA and grow the size of the xap or use mp3s with the very limited MediaElement functionalty, compromising on what you can implement with it.

You might be able to port some C# audio libraries to WP7, I haven't heard of any so far so it might be long shot.

In one of my apps I finally decided to go with the wav + XNA combination after playing around with different options for a good while.

Use the built-in XNA content pipeline sound effect compression!

The default setting for a SoundEffect content type is "Best" compression quality (which appears to, in fact, be no compression at all). Set the compression quality to "Low" and you will get a much, much smaller file. Or "Medium" for a nice balance between size and quality.

To change this setting, select your .wav file in the solution explorer, press F4 to bring up the properties window, expand the "Content Processor" node, and change the compression quality setting that appears.


Here are instructions with screenshots:

Create a new WP7 XNA game project (or otherwise get an XNA Content Project)

Add a wav file to the content project:

Press F4 with the wav file selected in the Solution Explorer to bring up the properties window.

Expand the "Content Processor" node and change the compression quality to the desired setting.

A setting of "Best" gives no compression (raw waveform), settings of "Medium" and "Low" give a much smaller file.

using Microsoft.Xna.Framework.Media;

void PlaySound(string pathToMp3) {
   Song song = Song.FromUri("name", new Uri(pathToMp3, UriKind.Relative));
   Microsoft.Xna.Framework.FrameworkDispatcher.Update();
   MediaPlayer.Play(song);
}

You could use MediaElement and set the source to the mp3, but this cannot be changed from code as in.

MediaElement me = sender as MediaElement;
me.Source = new Uri(

as you cannot load resources into the source. you could use multiple MediaElements in your xaml and stop them and start the ones you require. As long as you predetermined which files you wanted to load at compile time.

You could also combine those files into one and play them at a particular location, like so.

me.Position = new TimeSpan(0, 0, 0, 0, 1);
me.Play();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!