在小程序端,是用uni.getFileSystemManager()的writeFile方法把ArrayBuffer数据写成mp3格式音频播放的,在APP端不支持getFileSystemManager方法,
所以使用plus.io.requestFileSystem方法把ArrayBuffer数据写成mp3格式,但是写入成功的文件无法播放,下面是代码判断,有没大佬知道原因

- 发布:2024-01-22 17:42
- 更新:2025-02-08 11:27
- 阅读:706

麻烦问一下createWriter里面输出的返回值里面基本都是null,并且onwrite一直不触发是为什么,app的权限配置了,manifest也配置了读写权限
-
返回值是{
"fileName": "/storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/doc/temp.wav",
"readyState": 0,
"result": null,
"length": 0,
"position": 0,
"error": null,
"onwritestart": null,
"onprogress": null,
"onwrite": null,
"onabort": null,
"onsuccess": null,
"onerror": null,
"onwriteend": null
}2024-08-26 09:52

李朗 - 无障碍产品经理/工程师,从事各种无障碍产品的研发工作
<template>
<view class="content">
{{ imgPath }}
<image :src="'file://' + imgPath"></image>
<view class="text-area">
<!-- <text class="title">{{ title }}</text> -->
</view>
</view>
</template>
<script>
const innerAudioContext1 = uni.createInnerAudioContext();
const innerAudioContext2 = uni.createInnerAudioContext();
innerAudioContext1.autoplay = true;
innerAudioContext2.autoplay = true;
innerAudioContext1.onWaiting(() => {
console.log("播放器1加载");
});
innerAudioContext2.onWaiting(() => {
console.log("播放器2加载");
});
innerAudioContext1.onError((e) => {
console.log("播放器1错误", JSON.stringify(e));
});
innerAudioContext2.onError((e) => {
console.log("播放器2错误", JSON.stringify(e));
});
// innerAudioContext1.src =
// "https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3";
export default {
data() {
return {
imgPath: "", // 图片本地存储路径
imgBase64Data: "",
title: "Hello",
};
},
onLoad() {
this.getImg();
// this.testFile();
},
methods: {
getImg() {
const _this = this;
uni.request({
// url: "http://sky808.com/mpweimi.jpg",
url: "https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3",
responseType: "arraybuffer",
success(res) {
console.log("成功", res);
let base64String = uni.arrayBufferToBase64(res.data);
// _this.imgBase64Data = data:image/jpeg;base64, ${base64String}
;
// base64String = data:audio/mp3;base64, ${base64String}
;
// console.log(_this.imgBase64Data);
_this.testFile(base64String);
// _this.testFile(_this.res.data); // 直接保存二进制
},
});
},
testFile(base64Data) {
const _this = this;
// 获取本应用私有目录文件操作对象
plus.io.requestFileSystem(
// plus.io.PRIVATE_DOC,
plus.io.PUBLIC_DOCUMENTS,
function (fs) {
console.log("请求文件系统成功!");
fs.root.getFile("li.mp3", { create: true }, function (fileEntry) {
// 写文件-s
fileEntry.createWriter(
function (writer) {
writer.onwrite = function (e) {
console.log("写入数据成功!");
// const fileURL = fileEntry.toLocalURL();
const fileURL = fileEntry.toRemoteURL();
console.log("转换的url", fileURL);
console.log(JSON.stringify(e));
_this.imgPath = fileURL;
innerAudioContext1.src = fileURL;
console.log("播放器1src", innerAudioContext1.src);
innerAudioContext1.play();
innerAudioContext2.src = "file://" + e.target.fileName;
innerAudioContext2.src = "file:///test/西楼儿女.mp3";
innerAudioContext2.play();
console.log("播放器2src", innerAudioContext2.src);
console.log("播放器调用完成");
};
// 将数据写入文件末尾。
// writer.seek(writer.length);
writer.write(base64Data);
},
function (e) {
console.error(e.message);
}
);
// 写文件-e
fileEntry.file(function (file) {
var fileReader = new plus.io.FileReader();
console.log("获取的文件信息:" + JSON.stringify(file));
fileReader.readAsText(file, "utf-8");
fileReader.onloadend = function (evt) {
// console.log("加载结束:", JSON.stringify(evt));
};
console.log(file.size + "--" + file.name);
});
});
},
function (e) {
console.error("Request file system failed: " + e.message);
}
);
},
},
};
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>
1***@qq.com (作者)
谢谢大佬,解决了
2024-01-23 09:52
2***@qq.com
回复 1***@qq.com: 你这个问题和我好像,写入文件了之后怎么给他播放出来呢?
2024-05-10 16:38