我在小程序端实现了播放二进制音频,是先将二进制写为本地文件,然后播放,使用let fs = uni.getFileSystemManager();fs.writeFile()这个函数,但是这个在函数app端不支持,那么请问在app端怎样实现播放二进制音频呢?
- 发布:2023-04-22 19:30
- 更新:2024-01-26 10:07
- 阅读:1117
最佳回复
使用下面的临时方案, 实现了读写二进制文件: FileSystemManager
class Stream extends UniStream {
constructor() {
super()
}
readAsString(length, encoding, position) {
const arrayBuffer = new ArrayBuffer(length)
this._setPosition(position)
super.read(arrayBuffer, 0, length)
const text = new Encoding(encoding).getString(arrayBuffer)
return text
}
writeAsString(text, offset, length, encoding, position) {
const arrayBuffer = new Encoding(encoding).getBuffer(text)
return super.write(arrayBuffer, offset, arrayBuffer.byteLength)
}
_setPosition(position) {
if (typeof position === 'number' && position > -1) {
this.position = position
}
}
}
class FileStream {
constructor() {
this._map = new Map()
this._handle = 0
}
open(filePath, mode) {
const stream = this._newStream(filePath, mode)
this._handle++
this._map.set(this._handle, stream)
return this._handle
}
close(handle) {
const stream = this._getStream(handle)
if (stream) {
stream.close()
this._map.delete(handle)
}
}
read(handle, arrayBuffer, offset = 0, length = 0, position) {
const stream = this._getStream(handle)
if (stream) {
stream.read(arrayBuffer, offset, length)
}
}
write(handle, data, offset = 0, length = 0, encoding, position) {
const stream = this._getStream(handle)
if (stream) {
stream.write(data, offset, length)
}
}
_getStream(handle) {
return this._map.get(handle)
}
_newStream(filePath, mode) {
const stream = new Stream()
stream.open(filePath, mode)
return stream
}
}
////////////////
const fileStream = new FileStream()
class FileSystemManager {
constructor() {}
open(options) {
try {
options.success && options.success({
fd: this.openSync(options)
})
} catch (e) {
options.fail && options.fail({
errMsg: e.message
})
} finally {
options.complete && options.complete()
}
}
openSync(options) {
return fileStream.open(options.filePath, options.flag)
}
read(options) {
this._executeAsync('readSync', options)
}
readSync(options) {
const {
fd,
arrayBuffer,
offset,
length,
position
} = options
fileStream.read(fd, arrayBuffer, offset, length, position)
}
write(options) {
this._executeAsync('writeSync', options)
}
writeSync(options) {
const {
fd,
data,
offset,
length,
encoding,
position
} = options
fileStream.write(fd, data, offset, length, encoding, position)
}
close(options) {
this._executeAsync('closeSync', options)
}
closeSync(options) {
fileStream.close(options.fd)
}
// TODO 部分原生API暂不支持异步,暂时使用延时模拟以保证跨平台支持
_executeAsync(methodName, options) {
setTimeout(() => {
try {
const result = this[methodName](options)
if (options.success) {
options.success(result)
}
} catch (e) {
options.fail && options.fail({
errMsg: e.message
})
} finally {
options.complete && options.complete()
}
}, 1)
}
}
arrayBuffer转base64格式, 然后再把base64用plus.io.requestFileSystem写入本地,用mp3结尾就可以播放了
-
麻烦问一下plus.io.requestFileSystem写入本地的时候createWriter里面输出的返回值里面基本都是null,并且onwrite一直不触发是为什么,app的权限配置了,manifest也配置了读写权限,createWriter返回值是{
"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:56
jaycaoln
这个UniStream是从哪里拿到的呢
2023-07-25 00:01