0

I follow the instruction at: HTTP request with post to get the audio file from site: http://www.tudienabc.com/phat-am-tieng-nhat (This site allow us to input the english or japanese word/phrase/ sentence and generate the audio file, look like "/pronunciations/audio?file_name=1431134309.002.mp3&file_type=mp3" at line 129 of HTML code after postback).

However, the audio file which i get from my own application is not same with the one generated from this website. The audio file (mp3) generated from this website can play at www.tudienabc.com/pronunciations/ (such as: www.tudienabc.com/pronunciations/audio?file_name=1431141268.9947.mp3&file_type=mp3), but the audio file generated from my application can not play (such as: www.tudienabc.com/pronunciations/audio?file_name=1431141475.4908.mp3&file_type=mp3).

So, what wrong? And how to get the exact audio file?

Here is my code:

var request = (HttpWebRequest)WebRequest.Create("http://www.tudienabc.com/phat-am-tieng-nhat");

var postData = "_method=POST&data[Pronun][text]=hello&data[Pronun][type]=3";
var data = Encoding.ASCII.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

using (var stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

int m = responseString.IndexOf("pronunciations/audio?file_name=")+"pronunciations/audio?file_name=".Length;
int n = responseString.IndexOf("&file_type=mp3");
string filename = responseString.Substring(m, n - m);

return filename;

Thank you,

Community
  • 1
  • 1
htkhai
  • 1
  • 1
  • Show us the code you used. – Will B. May 09 '15 at 03:48
  • That is not "your" code, that is several methods to perform the same task. Post "your" code so we can see what is wrong with it. Rather than try to guess which option you chose to copy paste of a different answer, just to find out you accidentally deleted a comma when editing it. – Will B. May 09 '15 at 04:14
  • I am sorry, fyrye. This is first time I post the question to stackoverflow, so I have some trouble. When I press Enter to new line, it post my comment. :) I edited my question with my code because the comment not allow the long text. Thank you. – htkhai May 09 '15 at 04:19

1 Answers1

0

Their website processes the audio using ECMAScript

<script>
       var wait = new waitGenerateAudio(
         '#progress_audio_placeholder',
         '/pronunciations/checkFinish/1431151184.739',
         'aGVsbG8gZnlyeWU=',
         '/pronunciations/audio?file_name=1431151184.739.mp3&amp;file_type=mp3',
         'T?o file l?i'
       );
 </script>

You will need to be able to process the JavaScript for the audio file to be created.

Checkout C# httpwebrequest and javascript or WebClient runs javascript

For utilizing a headless browser.

I suggest looking into a more versatile library for text to audio. https://gist.github.com/alotaiba/1728771

Community
  • 1
  • 1
Will B.
  • 17,883
  • 4
  • 67
  • 69
  • Thank you. But according your suggest (stackoverflow.com/questions/516027/c-sharp-httpwebrequest-and-javascript): My code: wb.Navigate("http://www.tudienabc.com/phat-am-tieng-nhat", null, postdata, null); but the loop while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } can not finish. So, I can not get the response data. With the Google Text to Speech API, It can not return the audio follow the gender (male, female) and it is not native as my needed site. Do you have any way tackle my problem? Thanks, – htkhai May 09 '15 at 08:32