Сначала запустите фрагмент кода, затем прочитайте описание ... Это даст вам структуру
Я хочу записывать, воспроизводить и сохранять видео во 2-й video element
.Проблема, с которой я сталкиваюсь: поток запускается в 1-й video-element
, но не может записать и сохранить видео
.video {
border: 1px solid gray;
box-shadow: 3px 4px lightgray;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div style="text-align:center">
<h1>Welcome to WebRTC</h1>
<video class="video" #video autoplay controls></video>
<video class="video" style="width:360;" autoplay controls #recordedVideo></video>
<br>
<button class="btn btn-warning" (click)="startRecording()">Start</button>
<button class="btn btn-warning" (click)="stopRecording()">Stop</button>
<button class="btn btn-warning" (click)="playRecording()">Play</button>
</div>
export class ScreenRecordComponent implements OnInit {
@ViewChild('video') videoElementRef: ElementRef;
@ViewChild('recordedVideo') recordVideoElementRef: ElementRef;
recordMedia: MediaStream;
videoElement: HTMLVideoElement
recordVideoElement: HTMLVideoElement
async ngOnInit() {
this.videoElement = this.videoElementRef.nativeElement
this.recordVideoElement = this.recordVideoElementRef.nativeElement
navigator.mediaDevices.getUserMedia({ video: { width: 360 } }).then(stream => {
this.videoElement.srcObject = stream
})
}
async startRecording() {
navigator.mediaDevices.getUserMedia({ video: { width: 360 } }).then(stream => {
this.recordMedia = stream
this.videoElement.srcObject = stream
})
}
stopRecording() {
this.recordVideoElement.srcObject = this.recordMedia
this.recordMedia.getTracks().forEach(track => {
track.stop()
})
}
playRecording() {
this.recordVideoElement.srcObject = this.recordMedia
this.recordVideoElement.play()
}
}
------------------------ Изменена и решена проблема
То, что я сделал здесь, в коде Луиса Эстевеса , я объявил событие там в методе startRecording
, потому что когда я пытался протолкнуть stream-chunk в blob-массив, он ответил на ошибку: метод push не существует, даже я создал объект-массив после того, как объявил массив.
startRecording(stream) {
let options = { mimeType: 'video/webm' }
this.recordedBlobs = []
console.log(this.recordedBlobs)
try {
this.mediaRecorder = new MediaRecorder(stream, options)
} catch (e0) {
console.log('Try different mimeType')
}
console.log('Created MediaRecorder', this.mediaRecorder, 'with options', options)
// this.mediaRecorder.onstop = this.handleStop
this.mediaRecorder.onstop = (event) => {
console.log('Recorder stopped: ', event)
const videoBuffer = new Blob(this.recordedBlobs, { type: 'video/webm' })
this.downloadUrl = window.URL.createObjectURL(videoBuffer) // you can download with <a> tag
this.recordVideoElement = this.recordVideoElementRef.nativeElement
this.recordVideoElement.src = this.downloadUrl
}
// this.mediaRecorder.ondataavailable = this.handleDataAvailable
this.mediaRecorder.ondataavailable = (event) => {
if (event.data && event.data.size > 0) {
this.recordedBlobs.push(event.data)
}
}
this.mediaRecorder.start(100) // collect 100ms of data
console.log('MediaRecorder started', this.mediaRecorder)
}
Спасибо
Луис Эстевес :)