0

我正在尝试在Windows Phone 7应用程序的Silverlight中播放音频。我有一个MP3音频文件,其构建操作设置为资源。要播放声音,我使用:为什么Windows Media Player无法播放此音频?

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

此代码在SoundEffect.FromStream方法中引发InvalidOperationException。

回答

6

SoundEffect无法播放mp3文件。如果你想播放MP3文件,你应该使用这样的MediaPlayer

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

自己弄明白了。解决此问题的方法是使用.wav文件而不是.mp3。

+1

查看Cong Tran的替代方案 – msbg

相关问题