首页 > 其他分享 >WebAudioContext.createBufferSource

WebAudioContext.createBufferSource

时间:2025-01-06 10:23:58浏览次数:1  
标签:console buffer res audioCtx createBufferSource fail WebAudioContext

BufferSourceNode WebAudioContext.createBufferSource()

小程序插件:不支持

功能描述

创建一个BufferSourceNode实例,通过AudioBuffer对象来播放音频数据。

返回值

BufferSourceNode

示例代码

const audioCtx = wx.createWebAudioContext()

const loadAudio = (url) => {
  return new Promise((resolve) => {
    wx.request({
      url,
      responseType: 'arraybuffer',
      success: res => {
        console.log('res.data', res.data)
        audioCtx.decodeAudioData(res.data, buffer => {
          resolve(buffer)
        }, err => {
          console.error('decodeAudioData fail', err)
          reject()
        })
      },
      fail: res => {
        console.error('request fail', res)
        reject()
      }
    })
  })
}

const play = () => {
  loadAudio('xxx-test.mp3').then(buffer => {
    let source = audioCtx.createBufferSource()
    source.buffer = buffer
    source.connect(audioCtx.destination)
    source.start()
  }).catch(() => {
    console.log('fail')
  })
}

play()

标签:console,buffer,res,audioCtx,createBufferSource,fail,WebAudioContext
From: https://www.cnblogs.com/AtlasLapetos/p/18633202

相关文章