Created
March 20, 2010 15:18
-
-
Save cheeaun/338714 to your computer and use it in GitHub Desktop.
Say plugin for Talkerapp, which implements Google TTS. Just type '$say something'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://cheeaun.talkerapp.com/plugins/193 | |
var $style = $('<style>' | |
+ '.say-audio-control{' | |
+ 'text-decoration: none;' | |
+ 'background: #fff;' | |
+ 'color: #999;' | |
+ 'cursor: pointer;' | |
+ 'float: right;' | |
+ 'font-size: 10px;' | |
+ 'padding: .2em .8em;' | |
+ 'text-transform: uppercase;' | |
+ 'border-radius: 4px;' | |
+ '-moz-border-radius: 4px;' | |
+ '-webkit-border-radius: 4px;' | |
+ '}' | |
+ '.say-audio-control:hover{' | |
+ 'color: #333;' | |
+ '}' | |
+ '</style>'); | |
$('head').append($style); | |
var loaded = false; | |
plugin.onLoaded = function(){ | |
setTimeout(function(){ | |
loaded = true; | |
}, 2000); | |
}; | |
$.jPlayer.defaults.swfPath = '/flash/'; | |
$('.say-audio-control').live('click', function(){ | |
var el = $(this); | |
var text = el.text().toLowerCase(); | |
var audio = el.next('.say-audio:first'); | |
if (text == 'pause'){ | |
audio.jPlayer('pause'); | |
el.text('Play'); | |
} else { | |
audio.jPlayer('play'); | |
el.text('Pause'); | |
} | |
return false; | |
}); | |
plugin.onMessageInsertion = function(event){ | |
var lastIns = Talker.getLastInsertion(); | |
if (!lastIns.length) return; | |
var containsAudio = !!lastIns.data('say-audio'); | |
if (containsAudio) return; | |
var containsSay = lastIns.is(':contains($say )'); | |
if (!containsSay) return; | |
var text = lastIns.text(); | |
var query = $.trim(text.split('$say')[1]); | |
if (query == '') return; | |
var src = 'http://translate.google.com/translate_tts?tl=en&q=' + encodeURIComponent(query); | |
var id = (+new Date()); | |
var audio = $('<div class="say-audio" id="say-audio-' + id + '"></div>'); | |
lastIns.prepend(audio); | |
lastIns.prepend('<a href="#" class="say-audio-control">Play</a>'); | |
audio = $('#say-audio-' + id); | |
audio.jPlayer({ | |
ready: function(){ | |
var el = this.element; | |
el.jPlayer('setFile', src); | |
var control = el.prev('.say-audio-control:first'); | |
el.jPlayer('onSoundComplete', function(){ | |
control.text('Play'); | |
}); | |
if (loaded){ | |
el.jPlayer('play'); | |
control.text('Pause'); | |
} | |
el.data('say-audio', true); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment