function playVoice(arr, music) { //playFile 保存arr头一个音频文件 let playFile = arr.shift() //playFile 为空时结束语音播放 if (!playFile) { music = null // music.extractData() return } music.src = playFile music.play() music.onStop(function(res) { if (arr.length == 0) { music = null } else { playVoice(arr, music) } }) }
时间: 2024-04-05 18:32:29 浏览: 9
这是一段 JavaScript 代码,它定义了一个名为 `playVoice` 的函数,该函数接受两个参数:一个数组 `arr` 和一个音乐对象 `music`。该函数的作用是播放 `arr` 中的音频文件。
具体来说,该函数会从 `arr` 中取出第一个音频文件,并将其赋值给 `playFile` 变量。如果 `playFile` 为空,则说明 `arr` 中的所有音频文件已经播放完毕,此时将音乐对象 `music` 置为空并退出函数。
如果 `playFile` 不为空,则将其赋值给音乐对象的 `src` 属性,并调用 `play` 方法开始播放音频文件。同时,该函数还监听了音乐对象的 `onStop` 事件。当该事件被触发时,说明当前播放的音频文件已经播放完毕,此时会判断 `arr` 中是否还有未播放的音频文件。如果还有,则递归调用 `playVoice` 函数继续播放下一个音频文件;否则将音乐对象 `music` 置为空。
相关问题
uni-app如何获取用户的本机录音文件,并展示在页面中?
要获取用户本机的录音文件并展示在页面中,您可以使用uni-app提供的文件API进行文件读取和展示。以下是一个简单的示例代码:
```javascript
<template>
<div class="container">
<div class="header">录音列表</div>
<div class="list">
<div v-for="(item, index) in fileList" :key="index" class="item" @click="playAudio(item.path)">
<div class="name">{{ item.name }}</div>
<div class="duration">{{ item.duration }}s</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
fileList: [] // 录音文件列表
};
},
mounted() {
// 获取录音文件列表
this.getRecordList();
},
methods: {
// 获取录音文件列表
getRecordList() {
uni.getSavedFileList({
success: (res) => {
console.log('获取录音文件列表成功', res);
this.fileList = res.fileList.filter((item) => {
return item.filePath.indexOf('.silk') > -1; // 过滤出silk格式的录音文件
}).map((item) => {
return {
name: item.filePath.substring(item.filePath.lastIndexOf('/') + 1, item.filePath.lastIndexOf('.silk')),
path: item.filePath,
duration: Math.ceil(item.size / 1024 / 8) // 粗略计算录音时长
};
});
},
fail: (err) => {
console.error('获取录音文件列表失败', err);
}
});
},
// 播放录音
playAudio(path) {
uni.playVoice({
filePath: path,
success: () => {
console.log('播放录音成功');
},
fail: (err) => {
console.error('播放录音失败', err);
}
});
}
}
};
</script>
<style>
.container {
display: flex;
flex-direction: column;
height: 100vh;
}
.header {
height: 44px;
line-height: 44px;
text-align: center;
font-size: 16px;
color: #333;
}
.list {
flex: 1;
overflow: auto;
}
.item {
display: flex;
align-items: center;
justify-content: space-between;
height: 44px;
padding: 0 16px;
background-color: #fff;
border-bottom: 1px solid #e5e5e5;
font-size: 14px;
color: #666;
}
.name {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.duration {
margin-left: 12px;
}
</style>
```
以上代码会获取用户设备本地保存的录音文件列表,并展示在页面中,用户可以点击录音文件进行播放。需要注意的是,用户在录音时需要授权uni-app使用麦克风,并且需要过滤出指定格式的录音文件进行展示。
微信础库版本2.27.3微信授权登录,请用代码显示出来
以下是使用微信授权登录的示例代码:
```javascript
// 引入微信JSSDK
import wx from 'weixin-js-sdk';
// 封装微信授权登录方法
function wxLogin() {
// 获取当前页面的URL
const url = window.location.href.split('#')[0];
// 发送请求获取微信授权配置信息
axios.get('/api/wechat/config', {
params: {
url: url
}
}).then(res => {
// 配置微信JSSDK
wx.config({
debug: false,
appId: res.data.appId,
timestamp: res.data.timestamp,
nonceStr: res.data.nonceStr,
signature: res.data.signature,
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'getLocation',
'openLocation',
'chooseWXPay',
'chooseImage',
'previewImage',
'uploadImage',
'downloadImage',
'getLocalImgData',
'startRecord',
'stopRecord',
'onVoiceRecordEnd',
'playVoice',
'pauseVoice',
'stopVoice',
'onVoicePlayEnd',
'uploadVoice',
'downloadVoice',
'translateVoice',
'openCard',
'scanQRCode',
'chooseCard',
'addCard',
'getBrandWCPayRequest',
'openEnterpriseRedPacket',
'getRecevieBizHongBaoRequest',
'openBusinessView',
'openBusinessWebview',
'invokeMiniProgramAPI',
'login'
]
});
// 监听微信授权登录按钮的点击事件
const loginBtn = document.querySelector('.wx-login-btn');
loginBtn.addEventListener('click', () => {
wx.login({
success: (res) => {
if (res.code) {
// 发送授权登录请求
axios.post('/api/wechat/login', {
code: res.code
}).then(res => {
// 授权登录成功后的逻辑
}).catch(err => {
// 授权登录失败后的逻辑
});
} else {
console.log('登录失败!' + res.errMsg);
}
}
});
});
}).catch(err => {
console.log(err);
});
}
```
注意:该示例代码使用了 Axios 库来发送请求,需要先安装并引入 Axios 库。另外,获取微信授权配置信息的接口需要自行搭建。