App.jsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
import React, { useState } from 'react'; import { Button, Container, Row, Col, Card } from 'react-bootstrap'; import { useReactMediaRecorder } from 'react-media-recorder'; import 'bootstrap/dist/css/bootstrap.min.css'; const App = () => { const [mediaType, setMediaType] = useState('video'); // Default to video const { status, startRecording, stopRecording, mediaBlobUrl, previewStream, } = useReactMediaRecorder({ video: mediaType === 'video' ? true : false, audio: true, screen: mediaType === 'screen' ? true : false, onStop: (blobUrl) => console.log('Recording stopped', blobUrl), }); const handleMediaChange = (e) => { setMediaType(e.target.value); }; return ( <Container className="mt-5"> <Row className="justify-content-center"> <Col md={8}> <h1 className="text-center mb-4">WebRTC Media Recorder</h1> {/* Media Type Selector */} <div className="mb-4"> <Button variant={mediaType === 'video' ? 'primary' : 'secondary'} onClick={() => setMediaType('video')} > Video </Button> <Button variant={mediaType === 'audio' ? 'primary' : 'secondary'} className="ml-3" onClick={() => setMediaType('audio')} > Audio </Button> <Button variant={mediaType === 'screen' ? 'primary' : 'secondary'} className="ml-3" onClick={() => setMediaType('screen')} > Screen </Button> </div> {/* Recording Status */} <div className="text-center mb-4"> <h5>Status: {status}</h5> </div> {/* Preview */} {previewStream && ( <div className="mb-4"> <Card> <Card.Body> <video ref={(ref) => { if (ref) { ref.srcObject = previewStream; } }} autoPlay muted playsInline width="100%" /> </Card.Body> </Card> </div> )} {/* Control Buttons */} <div className="text-center"> <Button variant="success" onClick={startRecording} disabled={status === 'recording'} className="mr-3" > Start Recording </Button> <Button variant="danger" onClick={stopRecording} disabled={status !== 'recording'} > Stop Recording </Button> </div> {/* Recorded Media */} {mediaBlobUrl && ( <div className="text-center mt-4"> <h5>Recorded Media:</h5> <a href={mediaBlobUrl} download="recording.webm"> <Button variant="primary">Download Recording</Button> </a> <video src={mediaBlobUrl} controls className="mt-3" width="100%" /> </div> )} </Col> </Row> </Container> ); }; export default App; |