更新记录
1.0.0(2022-09-01)
新版首发
平台兼容性
Android | iOS |
---|---|
× | 适用版本区间:9 - 16 |
原生插件通用使用流程:
- 购买插件,选择该插件绑定的项目。
- 在HBuilderX里找到项目,在manifest的app原生插件配置中勾选模块,如需要填写参数则参考插件作者的文档添加。
- 根据插件作者的提供的文档开发代码,在代码中引用插件,调用插件功能。
- 打包自定义基座,选择插件,得到自定义基座,然后运行时选择自定义基座,进行log输出测试。
- 开发完毕后正式云打包
付费原生插件目前不支持离线打包。
Android 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/android
iOS 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/ios
注意事项:使用HBuilderX2.7.14以下版本,如果同一插件且同一appid下购买并绑定了多个包名,提交云打包界面提示包名绑定不一致时,需要在HBuilderX项目中manifest.json->“App原生插件配置”->”云端插件“列表中删除该插件重新选择
KJ-SpeechSynthesizer
离线文本转语音、支持生成音频、下载或播放状态监听(ios)
使用
<template>
<view class="content">
<button type="primary" @click="init">初始化</button>
<button type="primary" @click="speakUtterance">开始播放/重新生成音频</button>
<button type="primary" @click="pauseSpeakingAtBoundary">暂停播放/生成</button>
<button type="primary" @click="continueSpeaking">继续播放/生成</button>
<button type="primary" @click="stopSpeakingAtBoundary">停止播放/生成</button>
<button type="primary" @click="writeUtterance">生成音频到本地路径</button>
<button type="primary" @click="isSpeaking">是否在播放</button>
<button type="primary" @click="isPaused">是否暂停播放</button>
<button type="primary" @click="getSpeechVoicesLanguage">获取支持播放的语音</button>
</view>
</template>
<script>
const KJSpeechSynthesizer = uni.requireNativePlugin('KJ-SpeechSynthesizer');
const KJSpeechSynthesizerWrite = uni.requireNativePlugin('KJ-SpeechSynthesizerWrite'); //注意:如果需要同时播放和生成音频,可以用这个组件
export default {
data() {
return {
title: 'Hello',
filePath: plus.io.convertLocalFileSystemURL("_doc/KJSpeechSynthesizer/test.caf")
}
},
onLoad() {
},
methods: {
init() {
var dic = {
"speechString": "i晋文公回家哦i个好玩", //文本
"usesApplicationAudioSession": true, //是否使用了音频会话, ios13及以上才支持
"mixToTelephonyUplink": true, //是否混合到电话上行链路 ios13及以上才支持
"language": "zh-CN", //语言
"rate": 1, //速率
"volume": 1, //音量, 0-1
"pitchMultiplier": 1, //声调, 0.5-2
"prefersAssistiveTechnologySettings": true, //是否辅助技术, ios14及以上才支持
"preUtteranceDelay": 0.0, //播放后的延
"postUtteranceDelay": 0.0 //播放前的延迟
}
KJSpeechSynthesizer.init(dic, (res) => {
console.log("init:" + JSON.stringify(res));
if (res.type == "init") {
console.log("初始化完成")
} else if (res.type == "didStartSpeechUtterance") {
console.log("开始播放/重新生成")
} else if (res.type == "didFinishSpeechUtterance") {
console.log("完成播放/生成")
//this.init();
} else if (res.type == "didPauseSpeechUtterance") {
console.log("暂停播放/生成")
} else if (res.type == "didContinueSpeechUtterance") {
console.log("继续播放/生成")
} else if (res.type == "didCancelSpeechUtterance") {
console.log("取消播放/生成")
} else if (res.type == "willSpeakRangeOfSpeechString") {
console.log("播放/生成文本的位置,第" + res.location + "字符")
}
})
KJSpeechSynthesizerWrite.init(dic, (res) => {
console.log("Writeinit:" + JSON.stringify(res));
if (res.type == "init") {
console.log("Write初始化完成")
} else if (res.type == "didStartSpeechUtterance") {
console.log("Write开始播放/重新生成")
} else if (res.type == "didFinishSpeechUtterance") {
console.log("Write完成播放/生成")
//this.init();
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
innerAudioContext.src = this.filePath;
innerAudioContext.onPlay(() => {
console.log('开始播放');
});
innerAudioContext.onError((res) => {
console.log(res.errMsg);
console.log(res.errCode);
});
} else if (res.type == "didPauseSpeechUtterance") {
console.log("Write暂停播放/生成")
} else if (res.type == "didContinueSpeechUtterance") {
console.log("Write继续播放/生成")
} else if (res.type == "didCancelSpeechUtterance") {
console.log("Write取消播放/生成")
} else if (res.type == "willSpeakRangeOfSpeechString") {
console.log("Write播放/生成文本的位置,第" + res.location + "字符")
}
})
},
speakUtterance() {
KJSpeechSynthesizer.speakUtterance((res) => {
console.log("speakUtterance:" + JSON.stringify(res))
});
},
pauseSpeakingAtBoundary() {
KJSpeechSynthesizer.pauseSpeakingAtBoundary({
"boundary": 0
}, (res) => {
console.log("pauseSpeakingAtBoundary:" + JSON.stringify(res))
});
},
continueSpeaking() {
KJSpeechSynthesizer.continueSpeaking((res) => {
console.log("continueSpeaking:" + JSON.stringify(res))
});
},
stopSpeakingAtBoundary() {
KJSpeechSynthesizer.stopSpeakingAtBoundary({
"boundary": 0
}, (res) => {
console.log("stopSpeakingAtBoundary:" + JSON.stringify(res))
});
},
writeUtterance() {
/**
* ios13及以上才支持
* */
var dic = {
"filePath": this.filePath,
"commonFormat": 3, //写入文件时使用的处理格式 0(Other) 1(Float32) 2(Float64) 3(Int16) 4(Int32)
"interleaved": false //是否使用交错处理格式
// "settings": {
// "AVLinearPCMBitDepthKey": 16,
// "AVLinearPCMIsBigEndianKey": 0,
// "AVLinearPCMIsFloatKey": 0,
// "AVLinearPCMIsNonInterleaved": 0,
// "AVNumberOfChannelsKey": 1,
// "AVSampleRateKey": 22050
// }
}
KJSpeechSynthesizerWrite.writeUtterance(dic, (res) => {
console.log("writeUtterance:" + JSON.stringify(res));
})
},
isSpeaking() {
KJSpeechSynthesizer.isSpeaking((res) => {
console.log("isSpeaking:" + JSON.stringify(res))
});
},
isPaused() {
KJSpeechSynthesizer.isPaused((res) => {
console.log("isPaused:" + JSON.stringify(res))
});
},
getSpeechVoicesLanguage() {
KJSpeechSynthesizer.getSpeechVoicesLanguage((res) => {
console.log("getSpeechVoicesLanguage:" + JSON.stringify(res))
});
}
}
}
</script>