Monday, November 26, 2012

pkrss source 之 chrome.tts试用

今天我的pkrss看到有文章说chrome有chrome.tts的接口,所以我写下以下测试source:

// http://developer.chrome.com/extensions/tts.html
var speaktts_chrome = {
'tts':null,

'support':function(){
if((typeof(chrome) != "undefined") && chrome && (typeof(chrome.tts) != "undefined") && chrome.tts )
return true;
return false;
},

'init':function(){

if(!this.support())
return;

this.tts = chrome.tts;

speaktts.tts = this;
},

'ttsList':null,
'getTTSList':function(cb){
if(!this.ttsList){
this.ttsList = [];
chrome.tts.getVoices(function(voices) {
var tts,ttslist,ttsitem;
for (var i = 0; i < voices.length; i++) {
tts = voices[i];
ttsitem = ttslist[tts.lang];
if(!ttsitem){
ttsitem = {'locale':tts.lang,'tts':[]};
speaktts_chrome.ttsList.push(ttsitem);
}
ttsitem.tts.push({"id":tts.extensionId,"name":tts.voiceName});
}
});
}else{
if(cb)
cb(this.ttsList);
}
},

'speak':function(id){
var s = pksetting.user.locale2tts[pksetting.user.locale];
if(s){
if(s!="")
s = "&tts=" + s;
}else{
s = "";
}

var info = pkrsslib.getItemById(id);

this.tts.speak(
info.title,{
'lang': 'zh-CN',
'onEvent': function(event) {
// event.type in
// 'start': The engine has started speaking the utterance.
// 'word': A word boundary was reached. Use event.charIndex to determine the current speech position.
// 'sentence': A sentence boundary was reached. Use event.charIndex to determine the current speech position.
// 'marker': An SSML marker was reached. Use event.charIndex to determine the current speech position.
// 'end': The engine has finished speaking the utterance.
// 'interrupted': This utterance was interrupted by another call to speak() or stop() and did not finish.
// 'cancelled': This utterance was queued, but then cancelled by another call to speak() or stop() and never began to speak at all.
// 'error': An engine-specific error occurred and this utterance cannot be spoken. Check event.errorMessage for details.
// event.charIndex current position
if(event.type == 'end'){
speaktts.playing = false;

if(speaktts.autonext)
pkrssspeak.speaknext();
}
}
});
},

'stop':function(){
if(this.tts.played)
this.tts.pause();
}
};



结果发现,人家只支持在chrome store package的应用程序中.


我就把它打包到我的原来的chrome包中加载我的目标网址.


结果它出现以下错误:

尝试安装此扩展程序时出现以下警告:
'tts' is only allowed for extensions, legacy packaged apps and packaged apps, and this is a hosted app.




然后在鼠标单击事件中增加请求权限代码.


chrome.permissions.request({permissions: ['*://localhost/*','ttsEngine']}, 
function(p1,p2){
});


说Error during permissions.request: '*://localhost/*' is not a recognized permission.


改成


chrome.permissions.request({permissions: ['ttsEngine']}, 
function(p1,p2){
});


说:Error during permissions.request: The optional permissions API does not support 'ttsEngine'.


把ttsEngine改成tts也是一样.





No comments: