开发
API

EffectCameraStream.offStop

基础库 2.19.0 开始支持本方法,低版本需做兼容处理

取消监听相机暂停事件。取消后 EffectCameraStream.onStop 中的回调参数将不被触发。

语法

EffectCameraStream.offStop(callback)

参数说明

function callback

回调参数

代码示例

预期表现:

  • 点击【暂停相机】后出现相机关闭提示弹窗。
  • 点击【取消监听相机暂停事件】,再次重复上述操作,不会出现相机关闭提示弹窗。
<canvas type="webgl" id="myCanvas"></canvas>
<button type="primary" bindtap="requestEffectCamera">创建相机</button>
<button type="primary" bindtap="play">打开相机</button>
<button type="primary" bindtap="stop">暂停相机</button>
<button type="primary" bindtap="offStop">取消监听相机暂停事件</button>
Page({
  onShow: function (options) {
    try {
      this.effectCameraStream = tt.createEffectCameraStream(this);
    } catch (error) {
      console.error("errror", error);
    }
    this.effectCameraStream.onError(this.onCameraError);
    this.effectCameraStream.onRequest(this.onRequest);
    this.effectCameraStream.onPlay(this.onPlay);
    this.effectCameraStream.onStop(this.onStop);
  },
  requestEffectCamera() {
    tt.createSelectorQuery()
      .select("#myCanvas")
      .node()
      .exec((res) => {
        console.log("select query", res);
        this.canvas = res[0].node;
        this.canvasCtx = this.canvas.getContext("webgl");

        this.effectCameraStream.request({
          orientation: "back",
        });
      });
  },
  onCameraError(error) {
    console.log("onError", error);
    const { type, errMsg } = error;
    tt.showModal({
      title: `onError ${type}`,
      content: errMsg,
    });
  },
  onRequest() {
    console.log("相机资源申请成功");
    this.effectCameraStream.play();
  },
  onPlay(cameraVideo) {
    this.cameraStatus = "play";
    console.log("相机数据", cameraVideo);
    tt.showToast({
      title: "相机打开",
    });
    this.paint();
  },
  onStop() {
    this.cameraStatus = "stop";
    tt.showToast({
      title: "相机暂停",
    });
  },
  offStop() {
    if (this.effectCameraStream) {
      this.effectCameraStream.offStop(this.onStop);
    }
  },
  play() {
    if (this.effectCameraStream) {
      this.effectCameraStream.play();
    }
  },
  stop() {
    if (this.effectCameraStream) {
      this.effectCameraStream.stop();
    }
  },
  paint() {
    this.effectCameraStream.paintTo({
      canvas: this.canvasCtx.canvas,
      dx: 0,
      dy: 0,
      sx: 0,
      sy: 0,
      success: () => {},
      fail: (error) => {
        console.error("paint fail", error);
      },
    });
    this.requestId = this.canvas.requestAnimationFrame(() => {
      this.cameraStatus === "play" && this.paint();
    });
  },
  onHide() {
    if (this.effectCameraStream) {
      this.canvas.cancelAnimationFrame(this.requestId);
      this.effectCameraStream.offRequest(this.onRequest);
      this.effectCameraStream.offPlay(this.onPlay);
      this.effectCameraStream.offStop(this.offStop);
      this.effectCameraStream.offError(this.onCameraError);
      this.effectCameraStream.dispose();
      this.effectCameraStream = null;
    }
  },
});

Bug & Tip

暂无。

点击纠错
评价此篇文档