Nhảy tới nội dung

CameraContext.updateMediaConstraints

Bắt đầu hỗ trợ ở phiên bản:

  • SDK: 2.39.0

Update cấu hình streaming.

Parameters

PropertyTypeDefaultRequiredDescriptionMinimum Version
mediaConstraintsMediaConstraintstrueCấu hình streaming.
import React, { useEffect, useRef } from "react";
import { Box, Button, Page } from "zmp-ui";
import api, { FacingMode, ZMACamera } from "zmp-sdk";

const HomePage = () => {
const cameraRef = (useRef < ZMACamera) | (null > null);
const videoRef = useRef < HTMLVideoElement > null;
useEffect(() => {
const videoElement = videoRef.current;
if (!videoElement) {
console.log("Media component not ready");
return;
}
if (!cameraRef.current) {
cameraRef.current = api.createCameraContext({
videoElement: videoElement,
mediaConstraints: {
width: 640,
height: 480,
facingMode: FacingMode.BACK,
audio: false,
},
});
}
}, []);

type STREAM_TYPE = {
audio: boolean,
video: boolean,
};
const startStream = async (type: STREAM_TYPE) => {
const camera = cameraRef.current;
const enableVideo = type.video;
const enableAudio = type.audio;
await camera?.updateMediaConstraints({
audio: enableAudio,
video: enableVideo,
});

if (!camera?.isUsing()) {
await camera?.start();
}
};
return (
<Page>
<Box>
<video
style={{ width: "100vw", height: "auto" }}
ref={videoRef}
muted
playsInline
webkit-playsinline
/>
</Box>

<Box mt={5} flex alignContent={"center"}>
<Box flex alignContent={"center"}>
<Button
size={"small"}
className="mb-2"
variant="primary"
onClick={async () => {
await startStream({ audio: true, video: false });
}}
>
Start Micro
</Button>
<Button
size={"small"}
className="mb-2"
variant="primary"
onClick={async () => {
await startStream({ audio: false, video: true });
}}
>
Start Camera
</Button>
<Button
size={"small"}
className="mb-2"
variant="primary"
onClick={async () => {
await startStream({ audio: true, video: true });
}}
>
Start All
</Button>
</Box>
</Box>
</Page>
);
};

export default HomePage;