9***@qq.com
9***@qq.com
  • 发布:2023-10-11 08:44
  • 更新:2023-10-11 14:06
  • 阅读:88

开发小程序,如何把本地音频文件进行base64编码

分类:uni-app

开发小程序,如何把本地音频文件进行base64编码之后得到字符串

2023-10-11 08:44 负责人:无 分享
已邀请:
喜欢技术的前端

喜欢技术的前端 - QQ---445849201

希望能够帮到你

<template>  
    <view class="content">  
        <button @click="getFile">获取音频</button>  
    </view>  
</template>  

<script>  

    export default {  
        data() {  
            return {  
                imgUrl: null  
            }  
        },  
        onLoad() {  

        },  
        methods: {  
            getFile() {  
                let _this = this  
                wx.chooseMessageFile({  
                    success(res) {  
                        _this.wxFormat(res.tempFiles[0].path)  
                    }  
                })  
            },  
            wxFormat(img) {  
                wx.request({  
                    url: img,  
                    method: 'GET',  
                    responseType: 'arraybuffer',  
                    success: function(res) {  
                        console.log(res);  
                        // 1.在这里你可以使用获取到的 Base64 编码进行操作  
                        const base64Str = 'data:audio/wav;base64,' + wx.arrayBufferToBase64(res.data);  
                        console.log(base64Str);  
                    },  
                    fail: function(error) {  
                        console.error(error);  
                        // 处理请求失败  
                    }  
                });  
            }  
        }  
    }  
</script>  

<style>  
    .buffer {  
        padding: 30rpx 0;  
        font-size: 32rpx;  
    }  
</style>

要回复问题请先登录注册