0

我正在使用 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 文件还需要哪些其他代码?

4

1 回答 1

1

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

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

奇怪的是,WMAFileReader没有构造函数将 aStream作为参数,但Mp3FileReader两者WaveFileReader都这样做。

于 2015-03-24T21:24:51.250 回答