Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function sendData(imgDesc)
- {
- $.ajax({
- url: apiHost + "/watson-tts",
- type: "get",
- dataType: "jsonp",
- jsonpCallback: "_callback",
- timeout: 15000,
- data: {
- text: imgDesc
- },
- success: function(response) {
- console.log(response);
- openAudio(response);
- },
- error: function(jqXHR, textStatus, errorThrown) {
- console.log('error ' + textStatus + " " + errorThrown);
- }
- });
- }
- function openAudio(wavFile)
- {
- // wavFile holds remote audio url which can be played directly through browser
- triggerPlay(wavUrl); //First solution :: playing remote audio file
- //downloadAudio(wavUrl); // Second solution : Download the audio file in android system and play
- }
- function triggerPlay(wavUrl){
- var a = document.createElement('a');
- var linkText = document.createTextNode(wavUrl);
- a.appendChild(linkText);
- a.title = wavUrl;
- a.href = wavUrl;
- a.target = '_blank';
- document.getElementById("recording-list").appendChild(a);
- a.click();
- }
- function downloadAudio(urlPath)
- {
- console.log('File downloading from ' + urlPath);
- var uri = encodeURI(urlPath);
- var ranNum = Math.floor((Math.random() * 10000) + 1);
- var fileTransfer = new FileTransfer();
- window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
- var store = cordova.file.dataDirectory;
- var fileName = 'myaudio' + ranNum + '.wav';
- var filePath = store + fileName;
- fileTransfer.download(
- uri,
- filePath,
- function (entry) {
- console.log("download complete: " + filePath);
- triggerPlay(filePath);
- },
- function (error) {
- console.log(error);
- },
- false,
- {
- headers: {
- "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
- }
- }
- );
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement