c#无法播放wav文件
问题描述:
试图创建带有文本框的软件,用户可以在其中输入单词或短语,程序将使用谷歌翻译“说出”这些单词并将该语音下载到wav或mp3文件,然后播放该文件。c#无法播放wav文件
一切工作正常,播放除外。文件将被加载到计算机中,但在播放时,Visual Studio会提供该文件不是有效的wav文件的错误。 当从电脑手动播放文件时,媒体播放器播放它很好!
private void Puhu(string Sana) {
WebClient web = new WebClient();
web.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 9.0; Windows;)");
string encstr = string.Empty;
string filename = "tts.wav";
string s = Sana;
encstr = Uri.EscapeDataString(s);
web.DownloadFile("http://translate.google.com/translate_tts?tl=fi&q=" + encstr, ".\\" + filename);
(new SoundPlayer(@"tts.wav")).Play(); // this line wont work!! Audio format is wrong!
}
答
也许你可以尝试使用Windows Media Player控件?
参见:http://msdn.microsoft.com/en-us/library/dd562851(v=vs.85).aspx
我记得,声音播放是你必须提供波形文件的格式样的具体..
答
因为你的文件下载的是MP3
文件。和System.Media.SoundPlayer
只支持WAV
文件:
从MSDN:
SoundPlayer
类支持从文件路径,URL,包含.wav文件的Stream,或嵌入的资源加载一个.wav文件包含一个.wav文件。