[英]Play .wma from memory stream using NAudio
我正在使用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
作為參數的構造函數,但是Mp3FileReader
和WaveFileReader
都可以。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.