从HTTP请求播放音频文件

问题描述:

我很新的C#,我似乎无法找到与我的代码的问题。我试图从谷歌翻译文本到语音网站的音频文件并播放,但我不断收到错误:从HTTP请求播放音频文件

An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll 
Additional information: The wave header is corrupt. 

可能有人让我知道什么问题是在我的代码?其方法是:

public static void PlayWord(string Query) 
    { 
     string GoogleTranslateURL = "http://translate.google.com/translate_tts?tl=en"; 
     System.Net.WebRequest req = System.Net.WebRequest.Create(GoogleTranslateURL + (string.IsNullOrEmpty(Query) ? "" : "&q=" + Query)); 

     using (var ms = new MemoryStream()) 
     { 
      using (Stream webStream = req.GetResponse().GetResponseStream()) 
      { 
       var buffer = new byte[4096]; 
       int read; 
       while (webStream != null && (read = webStream.Read(buffer, 0, buffer.Length)) > 0) 
        ms.Write(buffer, 0, read); 
      } 
      using (SoundPlayer player = new SoundPlayer(ms)) 
      { 
       if (ms.CanSeek) ms.Seek(0, System.IO.SeekOrigin.Begin); 
       player.Stream = null; 
       player.Stream = ms; 
       player.Play(); 
      } 
     } 
    } 
+0

你想的话转换为语音? – Yehia 2014-10-04 23:55:17

 string GoogleTranslateURL = @"http://translate.google.com/translate_tts?tl=en&q=pop"; 
     System.Net.WebRequest req = System.Net.WebRequest.Create(GoogleTranslateURL); 

     string a = req.GetResponse().ContentType; 


     using (Stream webStream = req.GetResponse().GetResponseStream()) 
     { 
      FileStream stream = new FileStream(@"C:\Projects\EverydayProject\pop.wav", FileMode.Create, System.Security.AccessControl.FileSystemRights.FullControl, FileShare.ReadWrite, 100, FileOptions.None); 

      webStream.CopyTo(stream); 
      stream.Close(); 
      webStream.Close(); 
     } 


     ProcessStartInfo startInfo = new ProcessStartInfo(); 
     startInfo.FileName = @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe"; 
     startInfo.Arguments = @"C:\Projects\EverydayProject\pop.wav"; 
     var process = Process.Start(startInfo); 

     if(!process.HasExited) 
     { 
      process.Refresh(); 
      Thread.Sleep(1000); 
     } 

     process.CloseMainWindow(); 
     process.Close(); 

     if (File.Exists(@"C:\Projects\EverydayProject\pop.wav")) 
      File.Delete(@"C:\Projects\EverydayProject\pop.wav"); 

的问题是不是在HTTP请求! 这是为我工作,问题是在SoundPlayer类。检查如何使用SoundPlayer。

+0

我觉得不好,因为'SoundPlayer'只能播放wav,而不能播放mp3。而且,如果OP没有VLC呢? – Tony 2014-10-05 01:54:38

+0

@Tony op认为问题出在Http请求中。请求返回与其他程序一起工作的有效wav文件。正因为如此,我张贴这个。他应该看看SoundPlayer是如何工作的。错误来自SoundPlayer。如果他没有VLC,他可以使用他想要的任何东西。 – mybirthname 2014-10-05 02:08:15

+0

@Tony你是对的SoundPlayer只能播放wav和内容类型的响应不是wav! – mybirthname 2014-10-05 02:17:00

Try:

Player.Stream.Position = 0;

SoundPlayer可以play WAV文件而已,在这里什么MSDN说:

The SoundPlayer class provides a simple interface for loading and playing a .wav file.

但谷歌与MP3文件的答复,所以尽量MediaPlayer代替:

var mp = new MediaPlayer(); 
mp.Open(new Uri(GoogleTranslateURL + (string.IsNullOrEmpty(Query) ? "" : "&q=" + Query))); 
mp.Play(); 
+0

MediaPlayer没有没有参数的构造函数。您需要添加字节[]。你应该修复这些代码或者我有点误解了。 – mybirthname 2014-10-05 02:16:20

+0

我试过你的代码,我把byte []放在MediaPlayer中,我有一个异常。 – mybirthname 2014-10-05 02:30:21

+0

@mybirthname对不起,但它有:http://msdn.microsoft.com/en-us/library/system.windows.media.mediaplayer.mediaplayer(v=vs.110).aspx我想你可能会用一些其他的'MediaPlayer'类。 – Tony 2014-10-05 03:30:27