请教小程序怎么保存音频文件,比如mp3,wav 这样的文件?
发布于 3 年前 作者 osun 11692 次浏览 来自 官方Issues

请教小程序怎么保存音频文件,比如mp3,wav 这样的文件?

2 回复

请求授权下载保存权限,然后就调用下载并保存到本地

https://developers.weixin.qq.com/miniprogram/dev/api/network/download/wx.downloadFile.html

const that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope'scope.writePhotosAlbum',
            fail(e) {
              console.error(e)
            },
            success () {
              that.downVideo()
            }
          })
        } else {
          that.downVideo()
        }
      }
    })
  },
  downVideo() {
    wx.showLoading({
      title'下载中',
      masktrue
    })
    const that = this
    wx.downloadFile({
      url: that.data.url,
      success (res) {
        if (res.statusCode === 200) {
          wx.saveVideoToPhotosAlbum({
            filePath: res.tempFilePath,
            success (res) {
              console.log(res.errMsg)
            },
            fail(res) {
              console.error(res)
            },
            complete() {
              wx.hideLoading()
            }
          })
        }
      },
      failfunction(err{
        console.error(err)
        wx.showToast({
          title'下载失败'
        })
      },
      complete() {
        wx.hideLoading()
      }
    })
  }
})
回到顶部