尝试使用Microsoft.Xna.Framework将wma文件加载为SoundEffect时发生InvalidOperationException

时间:2013-02-08 12:07:19

标签: c# audio xna windows-phone-8 soundeffect

尝试将WMA文件(无DRM)加载为声音效果时,我收到无效操作异常。我的代码:

    public void LoadSound(String SoundFilePath, out SoundEffect Sound)
    {

        Sound = null;

        try
        {
            // Holds informations about a file stream.
            StreamResourceInfo SoundFileInfo = App.GetResourceStream(new Uri(SoundFilePath, UriKind.Relative));

            // Create the SoundEffect from the Stream
            Sound = SoundEffect.FromStream(SoundFileInfo.Stream);
        }
        catch (NullReferenceException)
        {
            // Display an error message
            MessageBox.Show("Couldn't load sound " + SoundFilePath);
        }
    }

此行Sound = SoundEffect.FromStream(SoundFileInfo.Stream)

发生错误

我可以毫无问题地加载WAV文件。我不想将WMA文件转换为WAV,因为原始WMA文件大小只有352KB,但是当转换为WAV文件时,其大小增加到1788KB!

1 个答案:

答案 0 :(得分:1)

您无法在SoundEffects中使用wma或mp3声音。尝试这种方法很好:

using Microsoft.Xna.Framework.Media;
...
Song s = Song.FromUri("sound name", new Uri(@"Resources/Alarms/Alarm01.wma", UriKind.Relative));
MediaPlayer.Play(s);

另请不要忘记参考 Microsoft.Xna.Framework 库。