小黄人波比
小黄人波比
  • 发布:2025-08-19 16:50
  • 更新:2025-08-21 12:20
  • 阅读:63

uniappx中的camera组件,在录像过程中,如果退出了后台,有时会无法触发停止录像api

分类:uni-app x

uniappx中的camera组件,在录像过程中,如果退出了后台,这个时候我仍然需要获取到这段时间录像的信息,我调用停止录像api,但是有时候会无法触发

2025-08-19 16:50 负责人:无 分享
已邀请:
唐家三少

唐家三少 - 我要写代码

提供一下代码示例、平台、版本信息,以及操作步骤。

  • 小黄人波比 (作者)


    <template>

    <view class="video_container">

    <camera style="width: 100%; height: 400px" :resolution="'medium'" :device-position="devicePosition"

    flash="flash" :frame-size="frameSize" @stop="handleStop" @error="handleError"
    @initdone="handleInitDone">

    </camera>

    <button @tap="startRecord">录制</button>

    <button @tap="stopRecord">停止录制</button>

    </view>

    </template>


    <script lang="uts" setup>

    const devicePosition = ref<string>('back'); // 前后摄像头

    const flash = ref<string>('off'); // 闪光灯

    const frameSize = ref<string>('medium');

    function startRecord() {

    const context = uni.createCameraContext();

    context?.startRecord({

    timeout: 60 * 60,

    selfieMirror: false,

    timeoutCallback: (res : any) => {

    console.log("timeoutCallback", res);

    },

    success: (res : any) => {

    console.log("start record success", res);

    },

    fail: (res : any) => {

    console.log("start record fail", res);

    }

    } as CameraContextStartRecordOptions)

    }

    function stopRecord() {

    const context = uni.createCameraContext();

    context?.stopRecord({

    compressed: false,

    success: (res : CameraContextStopRecordResult) => {

    console.log("stop record success", res);

    },

    fail: (res : any) => {

    console.log("stop record fail", res);

    }

    } as CameraContextStopRecordOptions)

    }

    function handleStop(e : UniCameraStopEvent) {

    console.log("stop", e);

    stopRecord();

    }

    function handleInitDone(e : UniCameraInitDoneEvent) {

    console.log("initdone", e.detail);

    }

    function handleError(e : UniCameraErrorEvent) {

    console.log("error", e.detail);

    }

    onHide(() => {

    stopRecord();

    })

    </script>


    <style lang="scss" scoped>

    .video_container {

    width: 100%;

    height: 100%;

    }


    .footer_btn {  
    background-color: #000;
    position: fixed;
    bottom: 0px;
    left: 0px;
    width: 100%;
    padding: 64rpx 112rpx 190rpx;
    }

    </style> 操作系统:window11 手机型号华为Mate 50E 操作步骤:点击开始录像,后退出后台,调用stopRecord,有时会无法触发context?.stopRecord,有时候却可以

    2025-08-21 15:19

  • 小黄人波比 (作者)

    hbuilderx版本:4.75

    2025-08-21 15:20

要回复问题请先登录注册