Advertisement
Guest User

Untitled

a guest
Aug 30th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1.  
  2. function sendData(imgDesc)
  3. {
  4. $.ajax({
  5. url: apiHost + "/watson-tts",
  6. type: "get",
  7. dataType: "jsonp",
  8. jsonpCallback: "_callback",
  9. timeout: 15000,
  10. data: {
  11. text: imgDesc
  12. },
  13. success: function(response) {
  14. console.log(response);
  15. openAudio(response);
  16. },
  17. error: function(jqXHR, textStatus, errorThrown) {
  18. console.log('error ' + textStatus + " " + errorThrown);
  19. }
  20. });
  21. }
  22.  
  23. function openAudio(wavFile)
  24. {
  25. // wavFile holds remote audio url which can be played directly through browser
  26. triggerPlay(wavUrl); //First solution :: playing remote audio file
  27.  
  28. //downloadAudio(wavUrl); // Second solution : Download the audio file in android system and play
  29.  
  30. }
  31.  
  32. function triggerPlay(wavUrl){
  33. var a = document.createElement('a');
  34. var linkText = document.createTextNode(wavUrl);
  35. a.appendChild(linkText);
  36. a.title = wavUrl;
  37. a.href = wavUrl;
  38. a.target = '_blank';
  39. document.getElementById("recording-list").appendChild(a);
  40. a.click();
  41. }
  42.  
  43. function downloadAudio(urlPath)
  44. {
  45.  
  46. console.log('File downloading from ' + urlPath);
  47. var uri = encodeURI(urlPath);
  48. var ranNum = Math.floor((Math.random() * 10000) + 1);
  49. var fileTransfer = new FileTransfer();
  50.  
  51. window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
  52. var store = cordova.file.dataDirectory;
  53. var fileName = 'myaudio' + ranNum + '.wav';
  54. var filePath = store + fileName;
  55. fileTransfer.download(
  56. uri,
  57. filePath,
  58. function (entry) {
  59. console.log("download complete: " + filePath);
  60. triggerPlay(filePath);
  61. },
  62. function (error) {
  63. console.log(error);
  64.  
  65. },
  66. false,
  67. {
  68. headers: {
  69. "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
  70. }
  71. }
  72. );
  73. });
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement