1.0.2 • Published 1 year ago

cordova.plugin.ths.jtvoice.wake v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

简介

思路开发 Cordova插件ThsJtVoiceWake,是基于捷通SDK的Android平台的语音唤醒插件,可以实现在Android设备上通过语音唤醒的方式来唤醒应用。

安装

安装ThsJtVoiceWake插件

cordova plugin add cordova.plugin.ths.jtvoice.wake
或
npm install cordova.plugin.ths.jtvoice.wake --save

方法说明

cordova.plugins.ThsJtVoice.createSession(successCallback, errorCallback)

描述

创建会话

参数

  • successCallback: 回调函数,成功回调
  • errorCallback: 回调函数,失败回调

返回值

示例

cordova.plugins.ThsJtVoice.createSession(() => {
    console.log('createSession success');
}, () => {
    console.log('createSession error');
});

cordova.plugins.ThsJtVoice.closeSession(successCallback, errorCallback)

描述

关闭会话

参数

  • successCallback: 回调函数,成功回调
  • errorCallback: 回调函数,失败回调

返回值

示例

cordova.plugins.ThsJtVoice.closeSession(() => {
    console.log('closeSession success');
}, () => {
    console.log('closeSession error');
});

cordova.plugins.ThsJtVoice.startDetect(successCallback, errorCallback)

描述

开始监听唤醒

参数

  • successCallback: 回调函数,成功回调
  • errorCallback: 回调函数,失败回调

返回值

示例

cordova.plugins.ThsJtVoice.startDetect(() => {
    console.log('startDetect success');
}, () => {
    console.log('startDetect error');
});

cordova.plugins.ThsJtVoice.stopDetect(successCallback, errorCallback)

描述

停止监听唤醒

参数

  • successCallback: 回调函数,成功回调
  • errorCallback: 回调函数,失败回调

返回值

示例

cordova.plugins.ThsJtVoice.stopDetect(() => {
    console.log('stopDetect success');
}, () => {
    console.log('stopDetect error');
});

onWakeupInAndroidCallback

描述

唤醒事件回调

参数

  • onWakeupInAndroidCallback: 监听事件名称
  • errorCallback: 回调函数,失败回调

返回值

data: 唤醒事件数据,包含唤醒词等信息

示例

document.addEventListener('onWakeupInAndroidCallback', data => {
            console.log(data);
            alert(JSON.stringify(data));
        }, false);

其他事件回调类似,具体参考完整示例代码。

完整代码

HTML代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Your Title Here</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                background-color: #f4f4f4;
                margin: 0;
                padding: 0;
            }

            .app {
                text-align: center;
                margin-top: 50px;
            }

            h1 {
                color: #333;
            }

            .event {
                margin-top: 20px;
                font-weight: bold;
            }

            button {
                padding: 10px 20px;
                margin: 10px;
                font-size: 16px;
                border: none;
                border-radius: 5px;
                background-color: #4CAF50;
                color: white;
                cursor: pointer;
            }

            button:hover {
                background-color: #45a049;
            }

        </style>
    </head>
    <body>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
            <button id="coolMethodBtn">调用插件coolMethod方法</button>
            <button id="createSessionBtn">创建会话</button>
            <button id="closeSessionBtn">关闭会话</button>
            <button id="startDetectBtn">开始监听唤醒</button>
            <button id="stopDetectBtn">停止监听唤醒</button>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>

JavaScript代码

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var app = {
    // Application Constructor
    initialize: function () {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
        // 监听唤醒事件
        document.addEventListener('onWakeupInAndroidCallback', data => {
            console.log(data);
            alert(JSON.stringify(data));
        }, false);
        // 继续检测唤醒事件
        document.addEventListener('onResumeInAndroidCallback', data => {
            console.log(data);
            alert(JSON.stringify(data));
        }, false);
        // 唤醒错误监听事件
        document.addEventListener('onErrorInAndroidCallback', data => {
            console.log(data);
            alert(JSON.stringify(data));
        }, false);
        // 唤醒检测已停止监听
        document.addEventListener('onEndInAndroidCallback', data => {
            console.log(data);
            alert(JSON.stringify(data));
        }, false);
        // 唤醒检测启动监听
        document.addEventListener('onStartInAndroidCallback', data => {
            console.log(data);
            alert(JSON.stringify(data));
        }, false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function () {
        this.receivedEvent('deviceready');
    },

    // Update DOM on a Received Event
    receivedEvent: function (id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    },
    coolMethod: function () {
        console.log('This is a cool method');
        cordova.plugins.ThsJtVoiceWake.coolMethod('123', () => {
            console.log('coolMethod success');
        }, () => {
            console.log('coolMethod error');
        });
    }
};

app.initialize();
// 获取coolMethodBtn 按钮绑定点击事件
var coolMethodBtn = document.getElementById('coolMethodBtn');
coolMethodBtn.addEventListener('click', app.coolMethod);
// 获取createSessionBtn 按钮bang绑定点击事件
var createSessionBtn = document.getElementById('createSessionBtn');
createSessionBtn.addEventListener('click', () => {
    cordova.plugins.ThsJtVoiceWake.createSession(() => {
        console.log('createSession success');
    }, () => {
        console.log('createSession error');
    });
});
// 获取 closeSessionBtn 按钮绑定点击事件
var closeSessionBtn = document.getElementById('closeSessionBtn');
closeSessionBtn.addEventListener('click', () => {
    cordova.plugins.ThsJtVoiceWake.closeSession(() => {
        console.log('closeSession success');
    }, () => {
        console.log('closeSession error');
    });
});
// 获取 startDetectBtn 按钮绑定点击事件
var startDetectBtn = document.getElementById('startDetectBtn');
startDetectBtn.addEventListener('click', () => {
    cordova.plugins.ThsJtVoiceWake.startDetect(() => {
        console.log('startDetect success');
    }, () => {
        console.log('startDetect error');
    });
});
// 获取 stopDetectBtn 按钮绑定点击事件
var stopDetectBtn = document.getElementById('stopDetectBtn');
stopDetectBtn.addEventListener('click', () => {
    cordova.plugins.ThsJtVoiceWake.stopDetect(() => {
        console.log('stopDetect success');
    }, () => {
        console.log('stopDetect error');
    });
});

注意事项

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago