赞
踩
app录音上传文件可以采用uniapp的方法,地址:uni.getRecorderManager() | uni-app官网
代码如下:
- const recorderManager = uni.getRecorderManager();
- const innerAudioContext = uni.createInnerAudioContext();
-
- innerAudioContext.autoplay = true;
- data() {
- return {
- text: 'uni-app',
- voicePath: ''
- }
- },
- onLoad() {
- let self = this;
- recorderManager.onStop(function (res) {
- console.log('recorder stop' + JSON.stringify(res));
- self.voicePath = res.tempFilePath;
- });
- },
这里onload里面这个res.tempFilePath返回的是文件路径可以配合uni.uploadFile方法进行上传
- recorderManager.onStop(function(res) {
- let token = self.$tool.localStorage.get('user_token');
- let imgHeader = {
- 'Authorization': 'Bearer ' + token,
- 'platform': 'app'
- };
- uni.uploadFile({
- url: self.$config.SERVERURL + "/proposal/XXX", (上传接口实例)
- filePath:res.tempFilePath,
- name: 'file',
- header: imgHeader,
- formData: {
- 'proposalId': self.proposalObj.proposalId
- },
- success: (uploadFileRes) => {
- console.log(uploadFileRes.data);
- self.tapeUrl = JSON.parse(uploadFileRes.data).data;
- },
- fail(err) {
- console.log(err);
- }
- });
- });

然后就是开始录音 结束录音 播放录音的事件函数了
- methods: {
- startRecord() {
- console.log('开始录音');
-
- recorderManager.start();
- },
- endRecord() {
- console.log('录音结束');
- recorderManager.stop();
- },
- playVoice() {
- console.log('播放录音');
-
- if (this.voicePath) {
- innerAudioContext.src = this.voicePath;
- innerAudioContext.play();
- }
- }
- }

以上根据实际开发需求更改。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。