当前位置 : 主页 > 手机开发 > cordova >

cordova – Phonegap TTS插件Android无法正常工作

来源:互联网 收集:自由互联 发布时间:2021-06-10
我正在使用 https://github.com/domaemon/org.apache.cordova.plugin.tts的TTS插件但该插件似乎不起作用.它甚至没有初始化. 安装如下插件(PHONEGAP 3.3) phonegap plugin add https://github.com/domaemon/org.apache.cordov
我正在使用 https://github.com/domaemon/org.apache.cordova.plugin.tts的TTS插件但该插件似乎不起作用.它甚至没有初始化.

安装如下插件(PHONEGAP 3.3)

phonegap plugin add https://github.com/domaemon/org.apache.cordova.plugin.tts.git
phonegap build android

在phonegap config.xml中添加了以下内容

<gap:plugin name="org.apache.cordova.plugins.tts" value="org.apache.cordova.plugins.tts"/>

在我的javascript中添加了以下代码

window.plugins.tts.startup(startupWin, fail);
function startupWin(result) {
    console.log("Startup win");
    // When result is equal to STARTED we are ready to play
    if (result == TTS.STARTED) {
        window.plugins.tts.getLanguage(win, fail);
        window.plugins.tts.speak("The text to speech service is ready");
        window.plugins.tts.isLanguageAvailable("en_US", function() {
            addLang("en_US", "English (American)");
        }, fail);
        window.plugins.tts.isLanguageAvailable("en_GB", function() {
            addLang("en_GB", "English (UK)");
        }, fail);
        window.plugins.tts.isLanguageAvailable("fr", function() {
            addLang("fr", "French");
        }, fail);
        window.plugins.tts.isLanguageAvailable("de", function() {
            addLang("de", "German");
        }, fail);
        window.plugins.tts.isLanguageAvailable("it", function() {
            addLang("it", "Italian");
        }, fail);
        window.plugins.tts.isLanguageAvailable("es", function() {
            addLang("es", "Spanish");
        }, fail);
    }
}

function addLang(loc, lang) {
    var langs = document.getElementById('langs');
    var langOption = document.createElement("OPTION") 
    langOption.innerText = lang; 
    langOption.value = loc;
    langs.options.add(langOption); 
}

function changeLang() {
    var yourSelect = document.getElementById('langs');
    window.plugins.tts.setLanguage(yourSelect.options[yourSelect.selectedIndex].value, win, fail);
}

function win(result) {
    console.log(result);
}

function fail(result) {
    console.log("Error = " + result);
}

function speak() {
    console.log("Speaking");
    window.plugins.tts.speak("How are you");
}

但是没有显示任何控制台日志消息.我在genymotion模拟器上测试它.

经过一番努力,我有TTS工作.但是我还有一个问题需要手动修复.以下是获得TTS工作的步骤

像下面安装插件.

phonegap plugin add https://github.com/domaemon/org.apache.cordova.plugin.tts.git
phonegap build android

一旦安装和构建.将此插件添加到phonegap config.xml文件中. (如果使用sencha touch构建应用程序,则config.xml将位于根文件夹中.)

<gap:plugin name="org.apache.cordova.plugins.tts" value="org.apache.cordova.plugins.tts"/>

这会将插件添加到最终版本中.现在,要启动TTS服务并说一些文本,请使用以下代码段.

navigator.tts.startup(startupWin, fail);
function startupWin(result) {
    console.log("Startup win");
    // When result is equal to STARTED we are ready to play
    console.log("Result "+result);
    //TTS.STARTED==2 use this once so is answered
    if (result == 2) {
        navigator.tts.getLanguage(win, fail);
        navigator.tts.speak("The text to speech service is ready");                                     
    }
}                               

function win(result) {
    console.log(result);
}

function fail(result) {
    console.log("Error = " + result);
}

我遇到的问题是在插件中未定义startupWin中的TTS.STARTED.我只是使用常量的值,插件工作得很好.

网友评论