开发
API

EffectCameraStream.dispose

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

关闭相机并释放相机资源,释放后将无法调用 EffectCameraStream 上的 API,如需调用,请通过 tt.createEffectCameraStream 重新创建。

语法

EffectCameraStream.dispose()

参数说明

返回值

代码示例

预期表现:依次点击【打开相机】-> 【释放相机资源】出现相机资源释放弹窗提示。

<button type="primary" bindtap="requestEffectCamera">打开相机</button>

<button type="primary" bindtap="disposeEffectCamera">释放相机资源</button>
Page({
  onShow: function () {},
  requestEffectCamera() {
    this.effectCameraStream = tt.createEffectCameraStream(this);
    this.effectCameraStream.onError(this.onError);
    this.effectCameraStream.onRequest(this.onRequest);
    this.effectCameraStream.onPlay(this.onPlay);
    this.effectCameraStream.request({
      orientation: "front",
    });
  },
  disposeEffectCamera() {
    if (this.effectCameraStream) {
      this.effectCameraStream.dispose();
      this.effectCameraStream.offError(this.onError);
      this.effectCameraStream.offRequest(this.onRequest);
      this.effectCameraStream.offPlay(this.onPlay);
      this.effectCameraStream = null;
      tt.showToast({
        title: "相机资源释放",
      });
    }
  },
  onError(error) {
    console.log("onError", error);
    const { type, errMsg } = error;
    tt.showModal({
      title: `onError ${type}`,
      content: errMsg,
    });
  },
  onRequest() {
    console.log("相机资源申请成功");
    this.effectCameraStream.play();
  },
  onPlay(cameraVideo) {
    tt.showToast({
      title: "相机打开",
    });
    console.log("相机数据", cameraVideo);
  },
});

Bug & Tip

暂无。

点击纠错
评价此篇文档