a***@163.com
a***@163.com
  • 发布:2023-04-22 19:30
  • 更新:2024-01-26 10:07
  • 阅读:901

请问怎样实现在app端播放二进制音频?

分类:uni-app

我在小程序端实现了播放二进制音频,是先将二进制写为本地文件,然后播放,使用let fs = uni.getFileSystemManager();fs.writeFile()这个函数,但是这个在函数app端不支持,那么请问在app端怎样实现播放二进制音频呢?

2023-04-22 19:30 负责人:无 分享
已邀请:

最佳回复

DCloud_uni-ad_HDX

DCloud_uni-ad_HDX

使用下面的临时方案, 实现了读写二进制文件: 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)  
  }  
}  
  • jaycaoln

    这个UniStream是从哪里拿到的呢

    2023-07-25 00:01

jaycaoln

jaycaoln - 111

楼主这个解决了吗。

MrMa

MrMa

同问UniStream是哪来的?

1***@qq.com

1***@qq.com

arrayBuffer转base64格式, 然后再把base64用plus.io.requestFileSystem写入本地,用mp3结尾就可以播放了

要回复问题请先登录注册