c# 无法播放wav文件

标签 c# audio media-player

尝试创建具有文本框的软件,用户可以在其中键入单词或短语,程序将使用谷歌翻译“说出”这些单词并将该语音下载到 wav 或 mp3 文件,然后播放该文件。

一切正常,除了播放。文件将加载到计算机,但在播放时,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!

  }

最佳答案

因为您下载的文件是MP3文件。和 System.Media.SoundPlayer仅支持 WAV 文件:

来自 MSDN:

SoundPlayer class supports loading a .wav file from a file path, a URL, a Stream that contains a .wav file, or an embedded resource that contains a .wav file.

关于c# 无法播放wav文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11499390/

相关文章:

c# - 自动化加载项与COM加载项

c# - IE 11 - webdriver.Quit() 不起作用

c# - 如何填充在每个索引中都有唯一数字的int数组?

c# - 是否可以自定义绘制 ListViewGroup

batch-file - 如何从lua脚本运行批生产线

调用 pause() 时 Android MediaPlayer IllegalStateExceptoion

android - 根据关联的音频文件拦截 Android 通知?

bash - Sox 批量转换

java - RTP音频流播放器

android 服务或 MediaPlayer 的自定义实现,哪个更好?