问题描述
我正在使用Visual Studio 2013中的C#应用程序,该应用程序需要播放.wav,.mp3和.wma格式的音频文件。 .wav和mp3文件没有问题。然而,.wma文件似乎需要额外处理,我无法找到解决方案。
以下是项目顶部的using语句文件:
使用 NAudio;
使用 NAudio.Wave;
使用 NAudio.FileFormats.Wav;
使用 NAudio.FileFormats.Mp3;
使用 NAudio.WindowsMediaFormat;
使用 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 = 新的 Mp3FileReader(msIntroAudioStream);
Player.Init(mp3rdr);
}
else if (GameInfo.IntroScreenAudioFileExt == .wma)
{
WMAFileReader wmafr = 新 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(),但应用程序崩溃,因为wmafr无法从字符串中读取。我需要什么其他代码才能播放.wma文件?
I'm working on a C# app in Visual Studio 2013 that needs to play audio files in .wav, .mp3 and .wma formats. the .wav and mp3 files play with no problem. .wma files, however, seem to require extra handling and I'm at a loss to find a solution.
Here are the using statements at the top of the project file:
using NAudio; using NAudio.Wave; using NAudio.FileFormats.Wav; using NAudio.FileFormats.Mp3; using NAudio.WindowsMediaFormat; using NAudio.MediaFoundation;
And here's the code for playback:
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; }
As you'll probably guess, I get a wiggly line under "(msIntroAudioStream)". I tried adding ".ToString()" inside the parentheses, but the app crashes, since wmafr can't read from a string. What other code do I need to play a .wma file?
这篇关于在C#中使用NAudio播放.wma文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!