项目初始化
This commit is contained in:
226
uni_modules/all-speech/js_sdk/h5-speech/speech.js
Normal file
226
uni_modules/all-speech/js_sdk/h5-speech/speech.js
Normal file
@@ -0,0 +1,226 @@
|
||||
class Recoder {
|
||||
constructor(sampleRate) {
|
||||
this.leftDataList = []
|
||||
this.rightDataList = []
|
||||
this.mediaPlayer = null
|
||||
this.audioContext = null
|
||||
this.source = null
|
||||
this.sampleRate = sampleRate || 44100
|
||||
}
|
||||
start() {
|
||||
return new Promise((resolve, reject) => {
|
||||
window.navigator.mediaDevices.getUserMedia({
|
||||
audio: {
|
||||
sampleRate: 8000, // 采样率
|
||||
channelCount: 1, // 声道
|
||||
audioBitsPerSecond: 64,
|
||||
volume: 1.0, // 音量
|
||||
autoGainControl: true
|
||||
}
|
||||
}).then(mediaStream => {
|
||||
console.log(mediaStream, 'mediaStream')
|
||||
this.mediaPlayer = mediaStream
|
||||
this.beginRecord(mediaStream)
|
||||
resolve()
|
||||
}).catch(err => {
|
||||
// 如果用户电脑没有麦克风设备或者用户拒绝了,或者连接出问题了等
|
||||
// 这里都会抛异常,并且通过err.name可以知道是哪种类型的错误
|
||||
console.error(err)
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
beginRecord(mediaStream) {
|
||||
let audioContext = new(window.AudioContext || window.webkitAudioContext)()
|
||||
// mediaNode包含 mediaStream,audioContext
|
||||
let mediaNode = audioContext.createMediaStreamSource(mediaStream)
|
||||
console.log(mediaNode, 'mediaNode')
|
||||
// 创建一个jsNode
|
||||
// audioContext.sampleRate = 8000
|
||||
console.log(audioContext, 'audioContext')
|
||||
let jsNode = this.createJSNode(audioContext)
|
||||
console.log(jsNode, 'jsnode')
|
||||
// 需要连到扬声器消费掉outputBuffer,process回调才能触发
|
||||
// 并且由于不给outputBuffer设置内容,所以扬声器不会播放出声音
|
||||
jsNode.connect(audioContext.destination)
|
||||
jsNode.onaudioprocess = this.onAudioProcess.bind(this)
|
||||
// 把mediaNode连接到jsNode
|
||||
mediaNode.connect(jsNode)
|
||||
this.audioContext = audioContext
|
||||
}
|
||||
|
||||
onAudioProcess(event) {
|
||||
console.log('is recording')
|
||||
// 拿到输入buffer Float32Array
|
||||
let audioBuffer = event.inputBuffer
|
||||
let leftChannelData = audioBuffer.getChannelData(0)
|
||||
// let rightChannelData = audioBuffer.getChannelData(1)
|
||||
|
||||
// 需要克隆一下
|
||||
this.leftDataList.push(leftChannelData.slice(0))
|
||||
//this.rightDataList.push(rightChannelData.slice(0))
|
||||
}
|
||||
|
||||
createJSNode(audioContext) {
|
||||
const BUFFER_SIZE = 4096
|
||||
const INPUT_CHANNEL_COUNT = 1
|
||||
const OUTPUT_CHANNEL_COUNT = 1
|
||||
// createJavaScriptNode已被废弃
|
||||
let creator = audioContext.createScriptProcessor || audioContext.createJavaScriptNode
|
||||
creator = creator.bind(audioContext)
|
||||
return creator(BUFFER_SIZE, INPUT_CHANNEL_COUNT, OUTPUT_CHANNEL_COUNT)
|
||||
}
|
||||
|
||||
playRecord(arrayBuffer) {
|
||||
let blob = new Blob([new Int8Array(arrayBuffer)], {
|
||||
type: 'audio/mp3' // files[0].type
|
||||
})
|
||||
let blobUrl = URL.createObjectURL(blob)
|
||||
this.source = blob
|
||||
this.blobUrl = blobUrl
|
||||
// document.querySelector(‘.audio-node‘).src = blobUrl
|
||||
return blobUrl
|
||||
}
|
||||
|
||||
stop() {
|
||||
// 停止录音
|
||||
let leftData = this.mergeArray(this.leftDataList)
|
||||
//let rightData = this.mergeArray(this.rightDataList)
|
||||
let allData = this.interSingleData(leftData)
|
||||
let wavBuffer = this.createWavFile(allData)
|
||||
|
||||
// 转到播放
|
||||
let source = this.playRecord(wavBuffer)
|
||||
// if (source) {
|
||||
// source = source.slice(5)
|
||||
// }
|
||||
console.log("我最后转换完播放的---------------------", source);
|
||||
this.resetRecord()
|
||||
return source
|
||||
}
|
||||
|
||||
transformArrayBufferToBase64(buffer) {
|
||||
var binary = ''
|
||||
var bytes = new Uint8Array(buffer)
|
||||
for (var len = bytes.byteLength, i = 0; i < len; i++) {
|
||||
binary += String.fromCharCode(bytes[i])
|
||||
}
|
||||
return window.btoa(binary)
|
||||
}
|
||||
|
||||
// 停止控件录音
|
||||
resetRecord() {
|
||||
this.leftDataList = []
|
||||
this.rightDataList = []
|
||||
this.audioContext.close()
|
||||
this.mediaPlayer.getAudioTracks().forEach(track => {
|
||||
track.stop()
|
||||
this.mediaPlayer.removeTrack(track)
|
||||
})
|
||||
}
|
||||
|
||||
createWavFile(audioData) {
|
||||
let channelCount = 1
|
||||
const WAV_HEAD_SIZE = 44
|
||||
const sampleBits = 16
|
||||
let sampleRate = this.sampleRate
|
||||
|
||||
let buffer = new ArrayBuffer(audioData.length * 2 + WAV_HEAD_SIZE)
|
||||
// 需要用一个view来操控buffer
|
||||
let view = new DataView(buffer)
|
||||
// 写入wav头部信息
|
||||
// RIFF chunk descriptor/identifier
|
||||
this.writeUTFBytes(view, 0, 'RIFF')
|
||||
// RIFF chunk length
|
||||
view.setUint32(4, 44 + audioData.length * channelCount, true)
|
||||
// RIFF type
|
||||
this.writeUTFBytes(view, 8, 'WAVE')
|
||||
// format chunk identifier
|
||||
// FMT sub-chunk
|
||||
this.writeUTFBytes(view, 12, 'fmt ')
|
||||
// format chunk length
|
||||
view.setUint32(16, 16, true)
|
||||
// sample format (raw)
|
||||
view.setUint16(20, 1, true)
|
||||
// stereo (2 channels)
|
||||
view.setUint16(22, channelCount, true)
|
||||
// sample rate
|
||||
view.setUint32(24, sampleRate, true)
|
||||
// byte rate (sample rate * block align)
|
||||
view.setUint32(28, sampleRate * 2, true)
|
||||
// block align (channel count * bytes per sample)
|
||||
view.setUint16(32, 2 * 2, true)
|
||||
// bits per sample
|
||||
view.setUint16(34, 16, true)
|
||||
// data sub-chunk
|
||||
// data chunk identifier
|
||||
this.writeUTFBytes(view, 36, 'data')
|
||||
// data chunk length
|
||||
view.setUint32(40, audioData.length * 2, true)
|
||||
|
||||
console.log(view, 'view')
|
||||
let length = audioData.length
|
||||
let index = 44
|
||||
let volume = 1
|
||||
for (let i = 0; i < length; i++) {
|
||||
view.setInt16(index, audioData[i] * (0x7FFF * volume), true)
|
||||
index += 2
|
||||
}
|
||||
return buffer
|
||||
}
|
||||
|
||||
writeUTFBytes(view, offset, string) {
|
||||
var lng = string.length
|
||||
for (var i = 0; i < lng; i++) {
|
||||
view.setUint8(offset + i, string.charCodeAt(i))
|
||||
}
|
||||
}
|
||||
|
||||
interSingleData(left) {
|
||||
var t = left.length;
|
||||
let sampleRate = this.audioContext.sampleRate,
|
||||
outputSampleRate = this.sampleRate
|
||||
sampleRate += 0.0;
|
||||
outputSampleRate += 0.0;
|
||||
var s = 0,
|
||||
o = sampleRate / outputSampleRate,
|
||||
u = Math.ceil(t * outputSampleRate / sampleRate),
|
||||
a = new Float32Array(u);
|
||||
for (let i = 0; i < u; i++) {
|
||||
a[i] = left[Math.floor(s)];
|
||||
s += o;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
// 交叉合并左右声道的数据
|
||||
interleaveLeftAndRight(left, right) {
|
||||
let totalLength = left.length + right.length
|
||||
let data = new Float32Array(totalLength)
|
||||
for (let i = 0; i < left.length; i++) {
|
||||
let k = i * 2
|
||||
data[k] = left[i]
|
||||
data[k + 1] = right[i]
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
mergeArray(list) {
|
||||
let length = list.length * list[0].length
|
||||
let data = new Float32Array(length)
|
||||
let offset = 0
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
data.set(list[i], offset)
|
||||
offset += list[i].length
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
export default Recoder
|
272
uni_modules/all-speech/js_sdk/wa-permission/permission.js
Normal file
272
uni_modules/all-speech/js_sdk/wa-permission/permission.js
Normal file
@@ -0,0 +1,272 @@
|
||||
/**
|
||||
* 本模块封装了Android、iOS的应用权限判断、打开应用权限设置界面、以及位置系统服务是否开启
|
||||
*/
|
||||
|
||||
var isIos
|
||||
// #ifdef APP-PLUS
|
||||
isIos = (plus.os.name == "iOS")
|
||||
// #endif
|
||||
|
||||
// 判断推送权限是否开启
|
||||
function judgeIosPermissionPush() {
|
||||
var result = false;
|
||||
var UIApplication = plus.ios.import("UIApplication");
|
||||
var app = UIApplication.sharedApplication();
|
||||
var enabledTypes = 0;
|
||||
if (app.currentUserNotificationSettings) {
|
||||
var settings = app.currentUserNotificationSettings();
|
||||
enabledTypes = settings.plusGetAttribute("types");
|
||||
console.log("enabledTypes1:" + enabledTypes);
|
||||
if (enabledTypes == 0) {
|
||||
console.log("推送权限没有开启");
|
||||
} else {
|
||||
result = true;
|
||||
console.log("已经开启推送功能!")
|
||||
}
|
||||
plus.ios.deleteObject(settings);
|
||||
} else {
|
||||
enabledTypes = app.enabledRemoteNotificationTypes();
|
||||
if (enabledTypes == 0) {
|
||||
console.log("推送权限没有开启!");
|
||||
} else {
|
||||
result = true;
|
||||
console.log("已经开启推送功能!")
|
||||
}
|
||||
console.log("enabledTypes2:" + enabledTypes);
|
||||
}
|
||||
plus.ios.deleteObject(app);
|
||||
plus.ios.deleteObject(UIApplication);
|
||||
return result;
|
||||
}
|
||||
|
||||
// 判断定位权限是否开启
|
||||
function judgeIosPermissionLocation() {
|
||||
var result = false;
|
||||
var cllocationManger = plus.ios.import("CLLocationManager");
|
||||
var status = cllocationManger.authorizationStatus();
|
||||
result = (status != 2)
|
||||
console.log("定位权限开启:" + result);
|
||||
// 以下代码判断了手机设备的定位是否关闭,推荐另行使用方法 checkSystemEnableLocation
|
||||
/* var enable = cllocationManger.locationServicesEnabled();
|
||||
var status = cllocationManger.authorizationStatus();
|
||||
console.log("enable:" + enable);
|
||||
console.log("status:" + status);
|
||||
if (enable && status != 2) {
|
||||
result = true;
|
||||
console.log("手机定位服务已开启且已授予定位权限");
|
||||
} else {
|
||||
console.log("手机系统的定位没有打开或未给予定位权限");
|
||||
} */
|
||||
plus.ios.deleteObject(cllocationManger);
|
||||
return result;
|
||||
}
|
||||
|
||||
// 判断麦克风权限是否开启
|
||||
function judgeIosPermissionRecord() {
|
||||
var result = false;
|
||||
var avaudiosession = plus.ios.import("AVAudioSession");
|
||||
var avaudio = avaudiosession.sharedInstance();
|
||||
var permissionStatus = avaudio.recordPermission();
|
||||
console.log("permissionStatus:" + permissionStatus);
|
||||
if (permissionStatus == 1684369017 || permissionStatus == 1970168948) {
|
||||
console.log("麦克风权限没有开启");
|
||||
} else {
|
||||
result = true;
|
||||
console.log("麦克风权限已经开启");
|
||||
}
|
||||
plus.ios.deleteObject(avaudiosession);
|
||||
return result;
|
||||
}
|
||||
|
||||
// 判断相机权限是否开启
|
||||
function judgeIosPermissionCamera() {
|
||||
var result = false;
|
||||
var AVCaptureDevice = plus.ios.import("AVCaptureDevice");
|
||||
var authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide');
|
||||
console.log("authStatus:" + authStatus);
|
||||
if (authStatus == 3) {
|
||||
result = true;
|
||||
console.log("相机权限已经开启");
|
||||
} else {
|
||||
console.log("相机权限没有开启");
|
||||
}
|
||||
plus.ios.deleteObject(AVCaptureDevice);
|
||||
return result;
|
||||
}
|
||||
|
||||
// 判断相册权限是否开启
|
||||
function judgeIosPermissionPhotoLibrary() {
|
||||
var result = false;
|
||||
var PHPhotoLibrary = plus.ios.import("PHPhotoLibrary");
|
||||
var authStatus = PHPhotoLibrary.authorizationStatus();
|
||||
console.log("authStatus:" + authStatus);
|
||||
if (authStatus == 3) {
|
||||
result = true;
|
||||
console.log("相册权限已经开启");
|
||||
} else {
|
||||
console.log("相册权限没有开启");
|
||||
}
|
||||
plus.ios.deleteObject(PHPhotoLibrary);
|
||||
return result;
|
||||
}
|
||||
|
||||
// 判断通讯录权限是否开启
|
||||
function judgeIosPermissionContact() {
|
||||
var result = false;
|
||||
var CNContactStore = plus.ios.import("CNContactStore");
|
||||
var cnAuthStatus = CNContactStore.authorizationStatusForEntityType(0);
|
||||
if (cnAuthStatus == 3) {
|
||||
result = true;
|
||||
console.log("通讯录权限已经开启");
|
||||
} else {
|
||||
console.log("通讯录权限没有开启");
|
||||
}
|
||||
plus.ios.deleteObject(CNContactStore);
|
||||
return result;
|
||||
}
|
||||
|
||||
// 判断日历权限是否开启
|
||||
function judgeIosPermissionCalendar() {
|
||||
var result = false;
|
||||
var EKEventStore = plus.ios.import("EKEventStore");
|
||||
var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(0);
|
||||
if (ekAuthStatus == 3) {
|
||||
result = true;
|
||||
console.log("日历权限已经开启");
|
||||
} else {
|
||||
console.log("日历权限没有开启");
|
||||
}
|
||||
plus.ios.deleteObject(EKEventStore);
|
||||
return result;
|
||||
}
|
||||
|
||||
// 判断备忘录权限是否开启
|
||||
function judgeIosPermissionMemo() {
|
||||
var result = false;
|
||||
var EKEventStore = plus.ios.import("EKEventStore");
|
||||
var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(1);
|
||||
if (ekAuthStatus == 3) {
|
||||
result = true;
|
||||
console.log("备忘录权限已经开启");
|
||||
} else {
|
||||
console.log("备忘录权限没有开启");
|
||||
}
|
||||
plus.ios.deleteObject(EKEventStore);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Android权限查询
|
||||
function requestAndroidPermission(permissionID) {
|
||||
return new Promise((resolve, reject) => {
|
||||
plus.android.requestPermissions(
|
||||
[permissionID], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
|
||||
function(resultObj) {
|
||||
var result = 0;
|
||||
for (var i = 0; i < resultObj.granted.length; i++) {
|
||||
var grantedPermission = resultObj.granted[i];
|
||||
console.log('已获取的权限:' + grantedPermission);
|
||||
result = 1
|
||||
}
|
||||
for (var i = 0; i < resultObj.deniedPresent.length; i++) {
|
||||
var deniedPresentPermission = resultObj.deniedPresent[i];
|
||||
console.log('拒绝本次申请的权限:' + deniedPresentPermission);
|
||||
result = 0
|
||||
}
|
||||
for (var i = 0; i < resultObj.deniedAlways.length; i++) {
|
||||
var deniedAlwaysPermission = resultObj.deniedAlways[i];
|
||||
console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
|
||||
result = -1
|
||||
}
|
||||
resolve(result);
|
||||
// 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
|
||||
// if (result != 1) {
|
||||
// gotoAppPermissionSetting()
|
||||
// }
|
||||
},
|
||||
function(error) {
|
||||
console.log('申请权限错误:' + error.code + " = " + error.message);
|
||||
resolve({
|
||||
code: error.code,
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// 使用一个方法,根据参数判断权限
|
||||
function judgeIosPermission(permissionID) {
|
||||
if (permissionID == "location") {
|
||||
return judgeIosPermissionLocation()
|
||||
} else if (permissionID == "camera") {
|
||||
return judgeIosPermissionCamera()
|
||||
} else if (permissionID == "photoLibrary") {
|
||||
return judgeIosPermissionPhotoLibrary()
|
||||
} else if (permissionID == "record") {
|
||||
return judgeIosPermissionRecord()
|
||||
} else if (permissionID == "push") {
|
||||
return judgeIosPermissionPush()
|
||||
} else if (permissionID == "contact") {
|
||||
return judgeIosPermissionContact()
|
||||
} else if (permissionID == "calendar") {
|
||||
return judgeIosPermissionCalendar()
|
||||
} else if (permissionID == "memo") {
|
||||
return judgeIosPermissionMemo()
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 跳转到**应用**的权限页面
|
||||
function gotoAppPermissionSetting() {
|
||||
if (isIos) {
|
||||
var UIApplication = plus.ios.import("UIApplication");
|
||||
var application2 = UIApplication.sharedApplication();
|
||||
var NSURL2 = plus.ios.import("NSURL");
|
||||
// var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
|
||||
var setting2 = NSURL2.URLWithString("app-settings:");
|
||||
application2.openURL(setting2);
|
||||
|
||||
plus.ios.deleteObject(setting2);
|
||||
plus.ios.deleteObject(NSURL2);
|
||||
plus.ios.deleteObject(application2);
|
||||
} else {
|
||||
// console.log(plus.device.vendor);
|
||||
var Intent = plus.android.importClass("android.content.Intent");
|
||||
var Settings = plus.android.importClass("android.provider.Settings");
|
||||
var Uri = plus.android.importClass("android.net.Uri");
|
||||
var mainActivity = plus.android.runtimeMainActivity();
|
||||
var intent = new Intent();
|
||||
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
||||
var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
|
||||
intent.setData(uri);
|
||||
mainActivity.startActivity(intent);
|
||||
}
|
||||
}
|
||||
|
||||
// 检查系统的设备服务是否开启
|
||||
// var checkSystemEnableLocation = async function () {
|
||||
function checkSystemEnableLocation() {
|
||||
if (isIos) {
|
||||
var result = false;
|
||||
var cllocationManger = plus.ios.import("CLLocationManager");
|
||||
var result = cllocationManger.locationServicesEnabled();
|
||||
console.log("系统定位开启:" + result);
|
||||
plus.ios.deleteObject(cllocationManger);
|
||||
return result;
|
||||
} else {
|
||||
var context = plus.android.importClass("android.content.Context");
|
||||
var locationManager = plus.android.importClass("android.location.LocationManager");
|
||||
var main = plus.android.runtimeMainActivity();
|
||||
var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
|
||||
var result = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER);
|
||||
console.log("系统定位开启:" + result);
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
judgeIosPermission: judgeIosPermission,
|
||||
requestAndroidPermission: requestAndroidPermission,
|
||||
checkSystemEnableLocation: checkSystemEnableLocation,
|
||||
gotoAppPermissionSetting: gotoAppPermissionSetting
|
||||
}
|
Reference in New Issue
Block a user