How do I use Bing translation service to play audio for specific words on android? -


i trying port wp7 app android. using bing translation service download & play audio specific words/phrases. how can in android? in bing, stream comes .wav file. here wp7 code:

private void button1_click(object sender, routedeventargs e)       {           this.speak();       }        public void speak()       {           string appid = "your id";           string text = "speak me";           string language = "en";            string uri = "http://api.microsofttranslator.com/v2/http.svc/speak?appid=" + appid +                   "&text=" + text + "&language=" + language + "&file=speak.wav";            webclient client = new webclient();           client.openreadcompleted += new openreadcompletedeventhandler(client_openreadcompleted);           client.openreadasync(new uri(uri));       }        void client_openreadcompleted(object sender, openreadcompletedeventargs e)       {           if (e.error != null) return;            var sound = e.result;           player.source = null;           string filename = "myaudio";           using (isolatedstoragefile userstoreforapplication = isolatedstoragefile.getuserstoreforapplication())           {               bool fileexists = userstoreforapplication.fileexists(filename);                if (fileexists)               {                   userstoreforapplication.deletefile(filename);               }                var isolatedstoragefilestream = userstoreforapplication.createfile(filename);                using (isolatedstoragefilestream)               {                   savefile(e.result, isolatedstoragefilestream);                    if (e.error == null)                   {                       player.setsource(isolatedstoragefilestream);                   }               }           }         }        public static void savefile(system.io.stream input, system.io.stream output)       {           try          {               byte[] buffer = new byte[32768];                while (true)               {                   int read = input.read(buffer, 0, buffer.length);                    if (read <= 0)                   {                       return;                   }                    output.write(buffer, 0, read);               }           }           catch (exception ex)           {               messagebox.show(ex.tostring());           }       }          void mysound_mediafailed(object sender, exceptionroutedeventargs e)       {           messagebox.show(e.errorexception.message);       }        void mysound_mediaopened(object sender, routedeventargs e)       {           player.play();       }  

i thought i'd drop generic response executing http requests , downloading files, more troublesome thing ran how microsoft azure wants authentication. evidently using app id deprecated , api extremely picky request headers , parameters.

in case, i'd suggest starting writing asynctask handles executing httpurlconnection. ended with:

/**  * tailor-made http request microsoft azure, downloading file  * specified location.  */ private class httpdownloadfile extends asynctask<string, integer, string> {      private string mdir;      @override     protected string doinbackground(string... params) {         if (params.length < 2) {             throw new illegalargumentexception(                     "two arguments required "                             + getclass().getsimplename());         }         string response = null;         string uri = params[0];         string query = params[1];         try {             if (query.length() > 0) {                 uri += "?" + query;             }             url url = new url(uri);             httpurlconnection connection = (httpurlconnection) url                     .openconnection();             connection.setrequestproperty("content-type",                     "application/x-www-form-urlencoded");             if (params.length > 2) {                 connection.setrequestproperty("authorization", "bearer "                         + params[2]);             }             connection.connect();             int filelength = connection.getcontentlength();             string charset = preferred_charset;             if (connection.getcontentencoding() != null) {                 charset = connection.getcontentencoding();             }              inputstream input;             outputstream output;             boolean iserror = false;             try {                 input = connection.getinputstream();                 output = new fileoutputstream(mdir);             } catch (ioexception e) {                 input = connection.geterrorstream();                 output = new bytearrayoutputstream();                 iserror = true;             }              byte data[] = new byte[1024];             long total = 0;             int count;             while ((count = input.read(data)) != -1) {                 total += count;                 publishprogress((int) (total * 100 / filelength));                 output.write(data, 0, count);             }              output.flush();             if (!iserror) {                 response = mdir;             } else {                 response = ((bytearrayoutputstream) output).tostring(charset);                 log.e(tag, response);                 response = null;             }             output.close();             input.close();         } catch (exception e) {             log.e(tag, "failed requesting " + uri, e);         }         return response;     } } 

you can execute task correct parameters:

httpdownloadfile task = new httpdownloadfile(); task.execute(         getstring(r.string.url_speak),         getstring(r.string.url_speak_query,                 text, tolanguage, file),         accesstoken,         environment.getexternalstoragedirectory().getpath()                 + "/temp.wav"); 

my strings.xml contains:

<string      name="url_speak"      formatted="false"      translate="false">http://api.microsofttranslator.com/v2/http.svc/speak</string> <string      name="url_speak_query"      formatted="false"      translate="false">text=%s&amp;language=%s&amp;file=%s</string> 

unfortunately, implies have code obtaining authentication token done. no worries! i've written complete code of that, too:

  1. mainactivity.java
  2. strings.xml

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -