async function publishVideo(client: IAgoraRTCClient) {
// Create a camera video track
const videoTrack = await AgoraRTC.createCameraVideoTrack();
try {
// Publish the video track to the channel
await client.publish(videoTrack);
console.log("Video publishing successful");
return videoTrack;
} catch (error) {
console.error("Error publishing video:", error);
throw error;
}
}
// Example usage with video controls
async function setupVideoInteraction(client: IAgoraRTCClient) {
let videoTrack;
// Start video
async function startVideo() {
try {
videoTrack = await publishVideo(client);
// Play the local video in a specific HTML element
videoTrack.play('local-video-container');
} catch (error) {
console.error("Failed to start video:", error);
}
}
// Stop video
async function stopVideo() {
if (videoTrack) {
// Stop and close the video track
videoTrack.stop();
videoTrack.close();
await client.unpublish(videoTrack);
videoTrack = null;
}
}
// Enable/disable video
function toggleVideo(enabled: boolean) {
if (videoTrack) {
videoTrack.setEnabled(enabled);
}
}
// Switch camera (if multiple cameras are available)
async function switchCamera(deviceId: string) {
if (videoTrack) {
await videoTrack.setDevice(deviceId);
}
}
return {
startVideo,
stopVideo,
toggleVideo,
switchCamera
};
}