WP7 play many compressed (mp3, wma etc) audio file

2019-01-28 07:19发布

问题:

For size reasons I need to bundle a WP7 app with compressed audio (mp3, wma etc). How do I play these freely/simultaneously?

The XNA framework only supports WAV files, so unless there is a pure C# managed code library somewhere to decompress mp3/wma/ogg (?) on the fly, the next option would be...

MediaElement. But I don't get good results with MediaElement. It seems that you need to add a MediaElement specifically as a tag in the xaml, and you can't use several instances (several tags). As soon as I play a certain MediaElement I can't play another MediaElement on the same page. I don't find anything about a restriction in the reference (the reference is very empty). I also tried dynamically creating MediaElement objects, but that doesn't seem valid at all, or I just cannot get it to play the files at all.

回答1:

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.



回答2:

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.



回答3:

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);
}


回答4:

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();