录音管理
web平台可通过插件拉齐,详见
编码格式与采样率、码率的关系
Android、iOS、微信小程序
| 采样率 | 编码码率 |
|---|---|
| 8000 | 16000 - 48000 |
| 11025 | 16000 - 48000 |
| 12000 | 24000 - 64000 |
| 16000 | 24000 - 96000 |
| 22050 | 32000 - 128000 |
| 24000 | 32000 - 128000 |
| 32000 | 48000 - 192000 |
| 44100 | 64000 - 320000 |
| 48000 | 64000 - 320000 |
HarmonyOS
encodeBitRate、sampleRate 和 numberOfChannels 不生效。| 类型 |
|---|
| RecorderManager |
start 开始录音
| 名称 | 类型 | 必填 | 兼容性 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| options | RecorderManagerStartOptions | 是 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pause 暂停录音,App-Android平台在Android 7.0及以后版本支持
resume 继续录音,App-Android平台在Android 7.0及以后版本支持
stop 停止录音
onStart 录音开始事件
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| options | (result: any) => void | 是 |
offStart 取消监听录音开始事件
onPause 录音暂停事件,App-Android平台在Android 7.0及以后版本支持
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| options | (result: any) => void | 是 |
offPause 取消监听录音暂停事件
onStop 录音停止事件,会回调文件地址
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| options | (result: RecorderManagerOnStopResult) => void | 是 |
| 名称 | 类型 | 必备 | 兼容性 | 描述 |
|---|---|---|---|---|
| tempFilePath | string | 是 | 录音文件的临时路径 |
offStop 取消监听录音停止事件
onFrameRecorded 已录制完指定帧大小的文件,会回调录音分片结果数据。如果设置了 frameSize ,则会回调此事件
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| options | (result: any) => void | 是 |
offFrameRecorded 取消监听帧回调事件
onError 录音错误事件, 会回调错误信息
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| options | (result: IRecorderManagerFail) => void | 是 |
| 名称 | 类型 | 必备 | 兼容性 | 描述 | |||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| errCode | number | 是 | 错误码 | ||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
| errSubject | string | 是 | 统一错误主题(模块)名称 | ||||||||||||||||||||||||||||
| data | any | 否 | 错误信息中包含的数据 | ||||||||||||||||||||||||||||
| cause | Error | 否 | 源错误信息,可以包含多个错误,详见SourceError | ||||||||||||||||||||||||||||
| errMsg | string | 是 | |||||||||||||||||||||||||||||
offError 取消监听录音错误事件
onResume 监听录音继续事件,App-Android平台在Android 7.0及以后版本支持
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| options | (result: any) => void | 是 |
offResume 取消监听录音继续事件
onInterruptionBegin 监听录音因为受到系统占用而被中断开始事件
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| options | (result: any) => void | 是 |
offInterruptionBegin 取消监听录音因为受到系统占用而被中断开始事件
onInterruptionEnd 监听录音中断结束事件
| 名称 | 类型 | 必填 | 兼容性 |
|---|---|---|---|
| options | (result: any) => void | 是 |
offInterruptionEnd 取消监听录音中断结束事件
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验
<template>
<page-head :title="title"></page-head>
<view class="page-body-time">
<text class="time-big">{{formatedRecordTime}}</text>
<!-- <text class="time-big">{{recordTimeInterval}}</text> -->
</view>
<scroll-view style="flex: 1;">
<view>
<button class="btnstyle" size="default" @click="registerOnStart">注册onStart</button>
<button class="btnstyle" size="default" @click="registeronStop">注册onStop</button>
<button class="btnstyle" size="default" id="btn-error" @click="registeronError">注册onError</button>
<button class="btnstyle" size="default" @click="registeronPause">注册onPause</button>
<button class="btnstyle" size="default" @click="registeronResume">注册onResume</button>
<button class="btnstyle" size="default" @click="registeronInterruptionBegin">注册onInterruptionBegin</button>
<button class="btnstyle" size="default" @click="registeronInterruptionEnd">注册onInterruptionEnd</button>
<view class="uni-list">
<text style="margin-bottom: 10px"> 请选择录音格式:</text>
<radio-group class="record-format-group" @change="radioChange">
<view class="record-format-item" v-for="(item, index) in items" :key="item.value">
<radio :value="item.value" :checked="index === current" />
<text>{{ item.name }}</text>
</view>
</radio-group>
</view>
<button class="btnstyle" size="default" :disabled="disableStartBtn" id="btn-startRecord"
@click="startRecord()">开始录制</button>
<button class="btnstyle" size="default" :disabled="disablePauseBtn" @click="pauseRecord">暂停录制</button>
<button class="btnstyle" size="default" :disabled="disableResumeBtn" @click="resumeRecord">继续录制</button>
<button class="btnstyle" size="default" id="btn-stopRecord" @click="stopRecord">停止录制</button>
<button class="btnstyle" size="default" id="btn-startPlay" @click="playVoice">开始播放</button>
<button class="btnstyle" size="default" id="btn-stopPlay" @click="stopVoice">停止播放</button>
</view>
<view class="scroll-view_bottom-fixed"></view>
</scroll-view>
</template>
<script setup lang="ts">
export type ItemType = { value : string, name : string }
type DataType = {
registerError: boolean,
recording: boolean,
playing: boolean
}
const disableStartBtn = ref(false)
const disableResumeBtn = ref(false)
const disablePauseBtn = ref(false)
const title = ref('start/stopRecord、play/stopVoice')
const hasRecord = ref(false) //是否有了一个
// 使用reactive避免ref数据在自动化测试中无法访问
const data = reactive({
registerError: false,
recording: false, //录音中
playing: false //播放中
} as DataType)
const playTimeInterval = ref(0)
const recordTimeInterval = ref(0)
const tempFilePath = ref('')
const recordTime = ref(0)
const current = ref(0)
const playTime = ref(0)
const formatedRecordTime = ref('00:00:00') //录音的总时间
const formatedPlayTime = ref('00:00:00')//播放录音的当前时间,
const recorderManager = ref(null as RecorderManager | null)
const music = ref(null as InnerAudioContext | null)
const items = ref([
{
value: 'aac',
name: 'aac'
},
{
value: 'mp3',
name: 'mp3'
},
{
value: 'wav',
name: 'wav'
},
// #ifdef APP-HARMONY
{
value: 'm4a',
name: 'm4a'
},
// #endif
// #ifndef APP-HARMONY
{
value: 'pcm',
name: 'pcm'
}
// #endif
] as ItemType[])
const radioChange = (e: UniRadioGroupChangeEvent) => {
for (let i = 0; i < items.value.length; i++) {
if (items.value[i].value === e.detail.value) {
current.value = i;
break;
}
}
}
const formatTime = (time: number): string => {
if (typeof time !== 'number' || time < 0) {
return time.toString()
}
var hour = parseInt((time / 3600).toString())
time = time % 3600
var minute = parseInt((time / 60).toString())
time = time % 60
var second = time
return [hour, minute, second].map((n: number) => {
let str = n.toString();
return str.length > 1 ? str : "0" + str;
}).join(":");
}
const registerOnStart = () => {
uni.showToast({
title: 'already registerOnStart'
})
recorderManager.value!.onStart(() => {
console.log('recorder on start');
recordTime.value = 0
data.recording = true;
recordTimeInterval.value = setInterval(() => {
recordTime.value += 1;
formatedRecordTime.value = formatTime(recordTime.value);
}, 1000)
});
}
const registeronStop = () => {
uni.showToast({
title: 'already registeronStop'
})
recorderManager.value!.onStop((res) => {
console.log('on stop', res);
music.value!.src = res.tempFilePath
clearInterval(recordTimeInterval.value)
hasRecord.value = true;
data.recording = false;
});
}
const registeronError = () => {
uni.showToast({
title: 'already registeronError'
})
data.registerError = true
recorderManager.value!.onError((res) => {
console.log('recorder onError', JSON.stringify(res));
});
}
const registeronPause = () => {
uni.showToast({
title: 'already registeronPause'
})
recorderManager.value?.onPause(() => {
console.log('recorder onPause');
})
}
const registeronResume = () => {
uni.showToast({
title: 'already registeronStop'
})
recorderManager.value?.onResume(() => {
console.log('recorder onResume');
})
}
const registeronOnFrameRecorded = () => {
uni.showToast({
title: 'already registeronOnFrameRecorded'
})
recorderManager.value?.onFrameRecorded((res) => {
console.log('recorder onFrameRecorded----', res);
})
}
const registeronInterruptionBegin = () => {
uni.showToast({
title: 'already registeronInterruptionBegin'
})
recorderManager.value?.onInterruptionBegin(() => {
console.log('recorder onInterruptionBegin');
})
}
const registeronInterruptionEnd = () => {
uni.showToast({
title: 'already registeronInterruptionEnd'
})
recorderManager.value?.onInterruptionEnd(() => {
console.log('recorder registeronInterruptionEnd');
})
}
const pauseRecord = () => {
console.log('recorder pause');
recorderManager.value?.pause()
if (data.recording) {
disableStartBtn.value = false
disablePauseBtn.value = true
disableResumeBtn.value = false
}
clearInterval(recordTimeInterval.value)
}
const resumeRecord = () => {
console.log('recorder resume ', recorderManager.value);
recorderManager.value?.resume()
recorderManager.value?.onResume(() => {
console.log('recorder onResume');
})
if (data.recording) {
disableStartBtn.value = false
disablePauseBtn.value = false
disableResumeBtn.value = true
recordTimeInterval.value = setInterval(() => {
recordTime.value += 1;
formatedRecordTime.value = formatTime(recordTime.value);
}, 1000)
}
}
const startRecord = () => { //开始录音
if (data.recording) {
uni.showToast({
title: disablePauseBtn.value ? "当前是录音暂停状态" : "当前正在录音"
})
return
}
console.log('startRecord', items.value[current.value].value)
// TODO ios 在没有请求过权限之前无法得知是否有相关权限,这种状态下需要直接调用录音,但没有状态或回调判断用户拒绝
recorderManager.value?.start({
format: items.value[current.value].value,
sampleRate: 8000,
numberOfChannels: 2,
encodeBitRate: 48000,
frameSize: 2
});
}
const stopRecord = () => { //停止录音
recorderManager.value?.stop();
disableStartBtn.value = false
disablePauseBtn.value = false
disableResumeBtn.value = false
}
const playVoice = () => {
if (data.recording) {
uni.showToast({
title: "当前录音还未结束"
})
return
}
console.log('play voice');
if (data.playing) {
return
}
data.playing = true;
playTimeInterval.value = setInterval(() => {
if (playTime.value < recordTime.value) {
playTime.value += 1;
}
formatedRecordTime.value = formatTime(playTime.value);
}, 1000)
music.value?.play();
}
const stopVoice = () => {
if (data.recording) {
uni.showToast({
title: "当前录音还未结束"
})
return
}
clearInterval(playTimeInterval.value)
data.playing = false;
formatedRecordTime.value = formatTime(0);
playTime.value = 0;
music.value?.stop();
}
const end = () => {
music.value?.stop();
music.value?.destroy();
// #ifdef APP
recorderManager.value?.offError()
recorderManager.value?.offFrameRecorded()
recorderManager.value?.offInterruptionBegin()
recorderManager.value?.offInterruptionEnd()
recorderManager.value?.offPause()
recorderManager.value?.offResume()
recorderManager.value?.offStart()
recorderManager.value?.offStop()
// #endif
recorderManager.value?.stop();
clearInterval(recordTimeInterval.value)
clearInterval(playTimeInterval.value);
data.recording = false
data.playing = false
hasRecord.value = false;
playTime.value = 0
recordTime.value = 0;
formatedRecordTime.value = "00:00:00"
formatedRecordTime.value = "00:00:00";
}
const clear = () => {
end();
}
onUnload(() => {
end();
})
onLoad(() => {
music.value = uni.createInnerAudioContext();
music.value!.onEnded(() => {
clearInterval(playTimeInterval.value)
var playTimeValue = 0
console.log('play voice finished')
data.playing = false;
formatedPlayTime.value = formatTime(playTimeValue);
playTime.value = playTimeValue;
});
recorderManager.value = uni.getRecorderManager();
recorderManager.value!.onStart(() => {
console.log('recorder onStart');
disableStartBtn.value = true
disablePauseBtn.value = false
disableResumeBtn.value = false
data.recording = true;
recordTime.value = 0
recordTimeInterval.value = setInterval(() => {
recordTime.value += 1;
formatedRecordTime.value = formatTime(recordTime.value);
}, 1000)
});
recorderManager.value!.onStop((res) => {
console.log('on stop', res.tempFilePath);
disablePauseBtn.value = false
disableResumeBtn.value = false
disableStartBtn.value = false
music.value!.src = res.tempFilePath
clearInterval(recordTimeInterval.value)
hasRecord.value = true;
data.recording = false;
});
recorderManager.value!.onError((res) => {
console.log('recorder onError', JSON.stringify(res));
disablePauseBtn.value = false
disableResumeBtn.value = false
disableStartBtn.value = false
data.registerError = true
uni.showToast({
title: JSON.stringify(res)
})
});
})
defineExpose({
data
})
</script>
<style>
.page-body-time {
display: flex;
flex-direction: column;
align-items: center;
}
.time-big {
font-size: 30px;
margin: 10px;
}
.btnstyle {
margin-left: 30px;
margin-right: 30px;
margin-top: 10px;
}
.uni-list {
border-bottom: 0px;
background-color: transparent;
margin-left: 30px;
margin-right: 30px;
margin-top: 10px;
margin-bottom: 10px;
}
.record-format-group {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.record-format-item {
display: flex;
flex-direction: row;
align-items: center;
margin-right: 15px;
}
</style>
ohos.permission.MICROPHONE。具体配置方法请参考鸿蒙权限配置指南| 名称 | 类型 | 必备 | 描述 |
|---|---|---|---|
| errMsg | string | 是 | 错误信息 |