Skip to content

Instantly share code, notes, and snippets.

@astagi
Created August 21, 2015 09:11
Show Gist options
  • Save astagi/284ab5bd048a21e7bf4f to your computer and use it in GitHub Desktop.
Save astagi/284ab5bd048a21e7bf4f to your computer and use it in GitHub Desktop.
Text to speech Angular Service
var app = angular.module('demo', ['textToSpeech']);
app.controller('demoCtrl', function($scope, $timeout, textToSpeech) {
textToSpeech.onVoiceReady(function(){
textToSpeech.speak(
'You worked 9 hours!!',
'UK English Male',
{pitch: 2}
).then(function() {
alert('Finish!');
}, function(err) {
alert('Error! ' + err);
});
});
});
<!DOCTYPE html>
<html ng-app="demo">
<head>
<meta charset="utf-8">
<title>ng-nephila demo</title>
<script src="lib/angular/angular.min.js"></script>
<script src='http://code.responsivevoice.org/responsivevoice.js'></script>
<script src="js/text-to-speech.js"></script>
<script src="js/appdemo.js"></script>
</head>
<body ng-controller="demoCtrl"></body>
</html>
angular.module('textToSpeech', [])
.factory('textToSpeech', ['$timeout', '$q', function($timeout, $q) {
var ready = false;
var readyCallback;
responsiveVoice.OnVoiceReady = function() {
if (ready === false) {
if (readyCallback !== undefined) {
readyCallback.call();
}
ready = true;
}
}
return {
isReady: function() {
return ready;
},
onVoiceReady: function(callback) {
readyCallback = callback;
},
speak: function(text, voice, options) {
var q = $q.defer();
if (voice === undefined) {
voice = 'UK English Male';
}
if (options === undefined) {
options = {};
}
options.onend = function() {
q.resolve();
}
$timeout(function() {
try {
responsiveVoice.speak(text, voice, options);
} catch (err) {
q.reject(err);
}
}, 1);
return q.promise;
}
}
}]);
@kastour
Copy link

kastour commented Jan 6, 2020

is there a demo with angular 6 ?
thanks for help

@astagi
Copy link
Author

astagi commented Jan 6, 2020

is there a demo with angular 6 ?
thanks for help

Hi @kastour there's no Angular 6 demo for TTS. This code is 4 years old, anyway Angular.js is just a wrapper, the core of textToSpeech is this library: http://code.responsivevoice.org/responsivevoice.js

You can make your own Angualr 6 application starting from scratch, you just need to:

If you need help, let me know!

AS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment