TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Property 'getTracks' does not exist on type 'MediaSource'.

Open alysonvilela opened this issue 3 years ago • 1 comments

According to MDN docs an HTMLMediaElement should have a srcObject that can use a function getTracks() which can be used to stop a media stream like the link below.

With the current srcObject type this function get an any type, so we need to infer using a as MediaStream to fix it.

// Stop errors
  function stopStreamedVideo(videoElem: HTMLMediaElement) {
    const stream = videoElem.srcObject as MediaStream
    const tracks = stream?.getTracks()

    tracks?.forEach((track) => {
      track.stop()
    })

    videoElem.srcObject = null
  }
// Show errors
  function stopStreamedVideo(videoElem: HTMLMediaElement) {
    const stream = videoElem.srcObject
    const tracks = stream?.getTracks()

    tracks?.forEach((track) => {
      track.stop()
    })

    videoElem.srcObject = null
  }

alysonvilela avatar Nov 28 '22 21:11 alysonvilela

https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject

The object can be a MediaStream, a MediaSource, a Blob, or a File (which inherits from Blob).

So srcObject is not always a MediaStream.

MartinJohns avatar Nov 28 '22 21:11 MartinJohns

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

typescript-bot avatar Dec 04 '22 20:12 typescript-bot