Home > front end >  document.pictureInPictureElement throws error in angular
document.pictureInPictureElement throws error in angular

Time:12-28

In my project I try to capture picture-in-picture(pip) activity using document.pictureInPictureElement. But it is returning following error:

error TS2339: Property 'pictureInPictureElement' does not exist on type 'Document'.
    
    438                 if(!document.pictureInPictureElement){

I tried document.pictureInPictureElement!== undefined, document.pictureInPictureElement!==null, in operator, hasownproperty() but nothing works. Everything returns the same error. Note: The typescript version I'm using is "typescript": "~3.6.4"

CodePudding user response:

If you are getting an error that the pictureInPictureElement property does not exist on the Document object, it could be due to a type definition issue. This error can occur if the TypeScript compiler does not have the correct type definitions for the Picture-in-Picture Web API, which define the properties and methods available on the Document object.

To fix this error, you can try installing the type definitions for the Picture-in-Picture Web API by running the following command in your project's root directory:

npm install --save @types/picture-in-picture

This will install the type definitions for the Picture-in-Picture Web API from the DefinitelyTyped repository, which should allow you to use the pictureInPictureElement property on the Document object without getting a type error.

I hope this helps resolve your issue.

CodePudding user response:

You can simply use the document.pictureInPictureEnabled boolean to check if Picture-in-Picture is supported and enabled in your web browser.

if ('pictureInPictureEnabled' in document) {
    if(document.pictureInPictureEnabled){
          console.log('Picture-in-Picture Web API is enabled');
    }
}
else {
  console.log('Picture-in-Picture Web API is not supported');
} 

For Picture-in-Picture support, we check if pictureInPictureEnabled property exists in the document object then we verify that the web API is enabled if equals true.

Ref: https://www.techiediaries.com/angular/picture-in-picture-javascript/
Code Sample: https://stackblitz.com/edit/angular-picture-in-picture-example?file=src/app/app.component.ts

  • Related