使用NAudio从内存流中播放.wma



我正在Visual Studio 2013中开发一个C#应用程序,该应用程序需要播放.wav、.mp3和.wma格式的音频文件。.wav和mp3文件播放起来没有问题。然而,wma文件似乎需要额外的处理,我无法找到解决方案。

以下是项目文件顶部的using语句:

using NAudio;
using NAudio.Wave;
using NAudio.FileFormats.Wav;
using NAudio.FileFormats.Mp3;
using NAudio.WindowsMediaFormat;
using NAudio.MediaFoundation;

这是播放的代码:

    private void PlayIntroScreenAudio()
    {
        Player.Stop();
        byte[] IntroAudioInBytes = Convert.FromBase64String(GameInfo.IntroScreenAudio);
        MemoryStream msIntroAudioStream = new MemoryStream(IntroAudioInBytes, 0, IntroAudioInBytes.Length);
        msIntroAudioStream.Write(IntroAudioInBytes, 0, IntroAudioInBytes.Length);
        msIntroAudioStream.Seek(0, SeekOrigin.Begin);
        msIntroAudioStream.Position = 0;
        if (GameInfo.IntroScreenAudioFileExt == ".wav")
        {
            WaveFileReader wfr = new WaveFileReader(msIntroAudioStream);
            Player.Init(wfr);
        }
        else if (GameInfo.IntroScreenAudioFileExt == ".mp3")
        {
            Mp3FileReader mp3rdr = new Mp3FileReader(msIntroAudioStream);
            Player.Init(mp3rdr);
        }
        else if (GameInfo.IntroScreenAudioFileExt == ".wma")
        {
            WMAFileReader wmafr = new WMAFileReader(msIntroAudioStream);
            Player.Init(wmafr);
        }
        Player.Play();
        IntroAudioIsPlaying = true;
        FinalScoreAudioIsPlaying = QuestionAudioIsPlaying = CARAudioIsPlaying = IARAudioIsPlaying = false;
        btnPlayIntroScreenAudio.Image = Properties.Resources.btnStopIcon;
        btnPlayFinalScoreAudio.Image = btnPlayQuestionAudio.Image = btnPlayCorrectResponseAudio.Image =
            btnPlayIncorrectResponseAudio.Image = Properties.Resources.btnPlayIcon;
        Player.PlaybackStopped += Player_PlaybackStopped;
    }

正如你可能猜到的那样,我在"(msIntroAudioStream)"下面得到了一条摆动的线。我试着在括号内添加".ToString()",但VS说这是错误的,因为wmafr不能从字符串中读取。我还需要什么代码来播放.wma文件?

WMAFileReader只支持来自文件的输入,并且它需要在其构造函数的参数中有一个表示文件路径的字符串。

如果要使用WMAFileReader,则必须先将MemoryStream写入文件,然后将路径提供给WMAFileReader

奇怪的是,WMAFileReader没有以Stream为参数的构造函数,但Mp3FileReaderWaveFileReader都有

最新更新