How to call <video> method in ionic


hi all,
i’m beginner to start with ionic, i have issue when call tag method in .ts file,
could you help me, how to call pause(), play(), and hanlding event ‘playing’, ‘paused’…



It’s the same as for Javascript unless you import a video library. Start here.


Thanks for your reply, but i have issue. this is my source code:


  <video id="mainVideo" fullscreen="fullscreen" autoplay="true" controls (cli)>
    <source src="assets/videos/" type="video/mp4">


    let mainVideo = document.getElementById('mainVideo');;
 } get error message is: Property ‘play’ does not exist on type ‘HTMLElement’

After search some thing by google, i find solution and want share detail for every one.

i modify some thing, at home.html file:

<ion-content padding>
  <video id="mainVideo" fullscreen="fullscreen" autoplay="true" controls>
  <button ion-button (click)="playVideo();"></button>

and at home.ts file

    let mainVideo = <HTMLMediaElement>document.getElementById('mainVideo');
    mainVideo.src = "file.mp4";;

read more here:

but i dont know, how to hanlder event in ts file like that: playing, paused, stop

const mainVideo: HTMLVideoElement = <HTMLVideoElement> document.getElementById('mainVideo');;


Thanks, but do you know, how to hanlde event, playing, paused,… some thing like that in .ts file?


You mean with addEventListener? Use HTMLVideoElement the way I did in that code snippet. It has available all the HTMLMediaElement events, including playing and paused. Use addEventListener the way it appears in the code snippet at the bottom of this page, with any event you want.

Edit: also, I recommend you take autoplay out of your video tag. A lot of devices won’t accept it. So it’s better if you assume that it isn’t there, and write your code accordingly.


i try like that, but it not working
this.mainVideo = document.getElementById(‘mainVideo’);
this.mainVideo.addEventListener(‘durationchange’, function () {

some event it working but for durationchange is not, it dont fire anything.


Maybe the duration doesn’t change?


<video #videoPlayer preload=“metadata” webkit-playsinline=“webkit-playsinline”>

import { ViewChild, ElementRef } from ‘@angular/core’;
@ViewChild(‘videoPlayer’) mVideoPlayer: ElementRef;
let video = this.mVideoPlayer.nativeElement;
video.onended = () => {
video.onerror = (e) => {
console.log('error ’ + JSON.stringify(video.error));
video.type = ‘video/mp4’;
video.src = ‘myvideo.mp4’;
video.oncanplay = () => {
//ok video is ready to play and duration is avaible
video.ontimeupdate = () => console.log(‘video time updated’);