-1

Is there a way in Angular 2 to make a video fullscreen once pressing a button?

This is the function I tried so far. (Note that pausing and playing works fine.)

fullscreen(video){
        video.requestFullscreen();
    }

HTML code

<video #video autoplay>
  <source src="assets/videos/mov_bbb.mp4" type="video/mp4">
   <img alt = "Test image" src="/assets/images/example.png" title="Your browser does not support the <video> tag">
   </video>
<button (click)="fullscreen(video);"> Fullscreen </button>
Fig
  • 275
  • 1
  • 7
  • 19
  • 1
    Possible duplicate of [Html5 Full screen video](https://stackoverflow.com/questions/6039909/html5-full-screen-video) –  Nov 09 '17 at 14:28
  • Possible duplicate of [this topic](https://stackoverflow.com/questions/6039909/html5-full-screen-video). You just need to adapt the solution to Angular, with a ViewChild. Do you know how to do that ? –  Nov 09 '17 at 14:29
  • I do not, I'm quiet new to angular sadly – Fig Nov 09 '17 at 14:30
  • I'm making you an answer right now then –  Nov 09 '17 at 14:31

2 Answers2

1

Following my comment, what you can do (and following this already answered topic) is something like this

@ViewChild('video') video: ElementRef;

toFullScreen() {
  let elem = this.video.nativeElement as HTMLVideoElement;
  if (elem.requestFullscreen) {
    elem.requestFullscreen();
  } else if (elem.mozRequestFullScreen) {
    elem.mozRequestFullScreen();
  } else if (elem.webkitRequestFullscreen) {
    elem.webkitRequestFullscreen();
  }
}
  • Where do I put @ViewChild('video') video: ElementRef;? also mozRequestFullScreen(); gives an error – Fig Nov 09 '17 at 14:38
  • You put it before the contrsuctor of your class, where you define your variables. And for the error, you will have to either deal with it, or post the stacktrace in your post so that I can help ! –  Nov 09 '17 at 14:39
1

Browsers have not yet broadly adopted support for the fullScreen API. So although your code looks ok, it won't behave as expected in some situations.

If this feature is important to your app I suggest using the FScreen polyfill, which is recommended by Mozilla.

  1. NPM install fscreen
  2. Import it into your component
  3. Modify your fullScreen() method to use it:

_

fullscreen(video){
  if(fscreen.fullscreenEnabled) {
      fscreen.requestFullscreen(video);
  } else {console.error('fullscreen is not supported');}
}

If you don't want to introduce a 3rd-party dependency, you may need to use the various prefixes that browsers expect. See the docs or adapt @trichetriche's answer

BeetleJuice
  • 39,516
  • 19
  • 105
  • 165