Why won't this Audio play in Windows Phone 7?

馋奶兔 提交于 2019-12-12 03:38:56

问题


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

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