开发
API
自2022年9月6日起,本文档站不再更新内容,相关文档已迁移至全新“抖音开放平台”前往

InnerAudioContext.onCanplay

基础库 1.0.0 开始支持本方法,这是一个同步方法。

监听音频进入可以播放状态,但不保证后面可以流畅播放。

语法

InnerAudioContext.onCanplay(callback)

参数说明

callback

类型默认值必填说明最低支持版本
function监听音频进入可播放状态的回调1.0.0

返回值

扫码体验

请使用字节宿主APP扫码

代码示例

开发者工具中预览

<view>
  <button bindtap="play">play</button>
  <button bindtap="pause">pause</button>
  <button bindtap="stop">stop</button>
  <button bindtap="seek">seek</button>
</view>
Page({
  data: {
    innerAudioCtx: undefined,
  },
  onLoad() {
    const innerAudioCtx = tt.createInnerAudioContext();
    innerAudioCtx.src = "https://tosv.byted.org/obj/developer-inner/qa/07.mp3";
    innerAudioCtx.obeyMuteSwitch = false;
    this.innerAudioCtx = innerAudioCtx;

    innerAudioCtx.onCanplay(() => {
      console.log("onCanplay");
    });

    innerAudioCtx.onPlay(() => {
      console.log("onPlay");
    });

    innerAudioCtx.onPause(() => {
      console.log("onPause");
    });

    innerAudioCtx.onStop(() => {
      console.log("onStop");
    });

    innerAudioCtx.onError((res) => {
      console.log(res);
    });
  },
  onUnload() {
    const innerAudioCtx = this.innerAudioCtx;
    if (innerAudioCtx) {
      innerAudioCtx.offCanplay();
      innerAudioCtx.destroy();
    }
  },
  play() {
    this.innerAudioCtx.play();
  },
  pause() {
    this.innerAudioCtx.pause();
  },
  stop() {
    this.innerAudioCtx.stop();
  },
  seek() {
    this.innerAudioCtx.seek(3);
  },
});

Bug & Tip

点击纠错
该文档是否对你的开发有所帮助?
有帮助
没帮助
该文档是否对你的开发有所帮助?
有帮助
没帮助