一 原生拉起录音授权,实时获取音频的返回的Float32arry数据
navigator.webkitGetUserMedia ||
try {
this.audioContext = new (window.AudioContext || window.webkitAudioContext)()
alert(‘浏览器不支持webAudioApi相关接口‘)
}
} catch (e) {
alert(‘浏览器不支持webAudioApi相关接口‘)
}
}
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
audio: {
},
})
})
.catch(e => {
getMediaFail(e)
})
} else if (navigator.getUserMedia) {
{
audio: {
},
},
},
function(e) {
getMediaFail(e)
}
)
} else {
if (navigator.userAgent.toLowerCase().match(/chrome/) && location.origin.indexOf(‘https://’) < 0) {
alert(‘chrome下获取浏览器录音功能,因为安全性问题,需要在localhost或127.0.0.1或https下才能获取权限‘)
} else {
alert(‘无法获取浏览器录音功能,请升级浏览器或使用chrome‘)
}
this.audioContext && this.audioContext.close()
}
let getMediaSuccess = stream => {
this.scriptProcessor = this.audioContext.createScriptProcessor(0, 1, 1)
this.scriptProcessor.onaudioprocess = e => {
getPCMarrayBuffer.push(e.inputBuffer.getChannelData(0).slice(0))
}
// 创建一个新的MediaStreamAudioSourceNode 对象,使来自MediaStream的音频可以被播放和操作
this.mediaSource = this.audioContext.createMediaStreamSource(stream)
// 连接
this.mediaSource.connect(this.scriptProcessor)
this.scriptProcessor.connect(this.audioContext.destination)
}
this.audioContext && this.audioContext.close()
}
}
npm install lamejs
import lamejs, { Mp3Enc } from ‘lamejs‘
/**
*/
/**
* 录音获得的 Float32Array 的数组转换成 Mp3 格式的 Blob 对象
*/
static float32ArrayToMp3(data: Float32Array[]): Blob {
const audioData = AudioConvert.mergeArray(data)
const int16Buffer = AudioConvert.floatArray2Int16(audioData)
return AudioConvert.encodeMono(1, 44100, int16Buffer)
}
/**
*/
private static mergeArray(list: Float32Array[]) {
const length = list.length * list[0].length
let data = new Float32Array(length)
for (let i = 0; i < list.length; i++) {
}
}
private static floatArray2Int16(floatBuffer: Float32Array) {
const int16Buffer = new Int16Array(floatBuffer.length)
for (let i = 0, len = floatBuffer.length; i < len; i++) {
int16Buffer[i] = 0x8000 * floatBuffer[i]
} else {
int16Buffer[i] = 0x7fff * floatBuffer[i]
}
}
}
private static encodeMono(channels: number, sampleRate: number, samples: Int16Array) {
const mp3enc: Mp3Enc = new lamejs.Mp3Encoder(channels, sampleRate, 128)
let remaining = samples.length
for (let i = 0; remaining >= maxSamples; i += maxSamples) {
const mono = samples.subarray(i, i + maxSamples)
const mp3buf = mp3enc.encodeBuffer(mono)
buffer.push(new Int8Array(mp3buf))
}
}
if (d.length > 0) {
}
// console.log(‘done encoding, size=’, buffer.length)
return new Blob(buffer, { type: ‘audio/mp3’ })
}
}
三 调用float32ArrayToMp3方法,录音获得的 Float32Array 的数组转换成 Mp3 格式的 Blob 对象;并播放
let blob = float32ArrayToMp3(getPCMarrayBuffer);
let blobUrl2 = window.URL.createObjectURL(blob)
document.getElementById(“audio”).src = blobUrl2;
document.getElementById(“audio”).play()
},100)
原文地址:https://blog.csdn.net/qq_40063198/article/details/132812454
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_33012.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!