问题
I am trying to play audio in my Silverlight for Windows Phone 7 app. I have an MP3 audio file with its build action set to resource. To play the sound, I use:
SoundEffectInstance sfi = null;
...
Stream source = Application.GetResourceStream(new Uri("/Bird Calls;component/Crow.mp3", UriKind.Relative)).Stream;
Microsoft.Xna.Framework.Audio.SoundEffect effect = SoundEffect.FromStream(source);
sfi = effect.CreateInstance();
sfi.Play();
This code throws a InvalidOperationException at the SoundEffect.FromStream method.
回答1:
SoundEffect can't play mp3 file. If you want to play mp3 file, you should use MediaPlayer like that
private Song song;
string musicUrl = string.Format("/Bird Calls;component/Crow.mp3");
song = Song.FromUri("name", new Uri(musicUrl, UriKind.Relative));
FrameworkDispatcher.Update();
MediaPlayer.IsRepeating = true;
MediaPlayer.Play(song);
回答2:
Figured this out myself. The solution to this problem is to use .wav files instead of .mp3's.
来源:https://stackoverflow.com/questions/13573854/why-wont-this-audio-play-in-windows-phone-7