更新记录

1.0.0(2025-09-25) 下载此版本

实现摆脱原生的阿里云实时语音识别


平台兼容性

aiSpeech

插件功能

专门用于app ios/android 的阿里云实时语音识别,不使用任何原生代码。

API 说明

1. this.$refs.aispeech.AiSpeech('start', appkey, token) - 开启录音并开始识别

start() {
    console.log('开始录音...');
    this.recordStatus = "录音中";
    this.content = ''
    //阿里云语音项目的appkey和token
    let appkey = "hWbe1YQxffY0CHRk"
    let token = "58dd24f07a444877a3836ac425dc74ec"
    this.$refs.aispeech.AiSpeech('start', appkey, token)
}

2. this.$refs.aispeech.AiSpeech('end') - 结束录音并结束识别

stop() {
    console.log('停止录音...');
    this.recordStatus = "已停止";
    this.$refs.aispeech.AiSpeech('end')
},

3. aiSay() - 实时是被返回的识别内容

aiSay(e) {
    console.log("识别结果:" + e.action);
    this.content = e.action
},

完整使用示例

录音权限被拒绝时的处理

<template>
    <view class="container">
        <view class="btn" @click="changeSpeech">
            {{isrecord?'查看麦克风权限-已打开':'麦克风权限-未打开'}}
        </view>
        <view class="btn" @click="start">
            开始录音
        </view>
        <view class="btn" @click="stop">
            停止录音
        </view>

        <view class="status">
            录音状态: {{ recordStatus }}
        </view>

        <view class="status">
            识别内容: {{ content }}
        </view>

        <aiSpeech ref="aispeech" @aiSay="aiSay" @endSpeech="endSpeech"></aiSpeech>

    </view>
</template>

<script>
    import {
        requestAndroidPermission,
        judgeIosPermissionRecord
    } from '/common/js/permission.js';
    export default {
        data() {
            return {
                str: "",
                text: "",
                recordStatus: "待开始",
                isrecord: false,
                content: ""
            }
        },
        onShow() {
            this.changeSpeech()
        },
        methods: {
            start() {
                console.log('开始录音...');
                this.recordStatus = "录音中";
                this.content = ''
                //阿里云语音项目的appkey和token
                let appkey = "hWbe1YQxffY0CHRk"
                let token = "58dd24f07a444877a3836ac425dc74ec"
                this.$refs.aispeech.AiSpeech('start', appkey, token)
            },
            stop() {
                console.log('停止录音...');
                this.recordStatus = "已停止";
                this.$refs.aispeech.AiSpeech('end')
            },

            endSpeech() {
                console.log("语音合成结束");
            },

            aiSay(e) {
                console.log("识别结果:" + e.action);
                this.content = e.action
            },

            //检查麦克风权限
            async changeSpeech() {
                let record = -1
                // #ifdef APP
                if (plus.os.name.toLowerCase() == 'android') {
                    record = await requestAndroidPermission('android.permission.RECORD_AUDIO', true, '麦克风权限')
                    // console.log("android麦克风权限", record);
                    if (record == 1) {
                        this.isrecord = true
                    } else {
                        this.isrecord = false
                    }
                }
                if (plus.os.name.toLowerCase() == 'ios') {
                    record = await judgeIosPermissionRecord(true, '麦克风权限')
                    // console.log("ios麦克风权限", record);
                    if (record == 1) {
                        this.isrecord = true
                    } else {
                        this.isrecord = false
                    }
                }
                // #endif

                // #ifdef H5
                this.isrecord = false
                // #endif

                this.$forceUpdate()
            }
        }
    }
</script>

<style>
    .container {
        padding: 20px;
        font-size: 14px;
        line-height: 24px;
    }

    .btn {
        background-color: #007aff;
        color: white;
        padding: 12px 24px;
        margin: 10px 0;
        border-radius: 8px;
        text-align: center;
        font-size: 16px;
        cursor: pointer;
    }

    .btn:active {
        background-color: #0056cc;
    }

    .status {
        margin: 20px 0;
        font-size: 16px;
        font-weight: bold;
        color: #333;
    }

    .pcm-info {
        margin: 10px 0;
        color: #666;
    }

    .last-data {
        margin: 10px 0;
        padding: 10px;
        background-color: #f5f5f5;
        border-radius: 4px;
        font-family: monospace;
        font-size: 12px;
        word-break: break-all;
    }
</style>

兼容性

  • iOS 8.0+
  • UTS框架
  • UniApp 应用

隐私、权限声明

1. 本插件需要申请的系统权限列表:

录音权限

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议