在C#中使用NAudio播放.wma文件 [英] Playing .wma files with NAudio in C#

查看:116
本文介绍了在C#中使用NAudio播放.wma文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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文件?

解决方案

这里有一些提示,因为你似乎是新手:



1.注意编译器消息

2.阅读文档

3.使用Google

4。如果一切都失败了,请转到正确的论坛。 https://naudio.codeplex.com/discussions [ ^ ]



这解释了WMAFileReader,可能会解决您的问题:

https://naudio.codeplex.com/discussions/578218 [ ^ ]

http://naudio.codeplex.com/discussions/243273 [ ^ ]



您将从讨论中注意到,即使您向构造函数提供文件名字符串后,仍可能存在问题WMAFileReader。

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?

解决方案

Here are some tips as you seem new to this:

1. Take notice of the compiler messages
2. Read the documentation
3. Use Google
4. If all else fails go to the correct forum. https://naudio.codeplex.com/discussions[^]

This explains WMAFileReader and will probably solve your problem:
https://naudio.codeplex.com/discussions/578218[^]
http://naudio.codeplex.com/discussions/243273[^]

You will note from the discussions that even after you supply a filename string to the constructor there may still be problems with WMAFileReader.


这篇关于在C#中使用NAudio播放.wma文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆