direct_tarun

SpeechRecognitionCode.java

Sep 17th, 2013
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataOutputStream;
  5. import java.io.InputStreamReader;
  6. import java.net.HttpURLConnection;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9. import java.nio.file.Files;
  10. import java.nio.file.Path;
  11. import java.nio.file.Paths;
  12.  
  13. public class TestGoogleApiForSpeechRecognition {
  14.  
  15. public static void main(String[] args) throws Exception {
  16.  
  17. Path path = Paths.get("C:\\Users\\CDAC\\Downloads\\priyanka.flac");
  18. byte[] data = Files.readAllBytes(path);
  19.  
  20. String request = "https://www.google.com/"+
  21. "speech-api/v1/recognize?"+
  22. "xjerr=0&client=speech2text&lang=en-US&maxresults=20";
  23. URL url = new URL(request);
  24. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  25. connection.setDoOutput(true);
  26. connection.setDoInput(true);
  27. connection.setInstanceFollowRedirects(false);
  28. connection.setRequestMethod("POST");
  29. connection.setRequestProperty("Content-Type", "audio/x-flac; rate=16000");
  30. connection.setRequestProperty("User-Agent", "speech2text");
  31. connection.setConnectTimeout(60000);
  32. connection.setUseCaches (false);
  33.  
  34. DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
  35. wr.write(data);
  36. wr.flush();
  37. wr.close();
  38. connection.disconnect();
  39.  
  40. System.out.println("Done");
  41.  
  42. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  43.  
  44. String decodedString;
  45. while ((decodedString = in.readLine()) != null) {
  46. System.out.println(decodedString);
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment