How do i stop music player when app is minimized and keep it checked on loop

so I'm not sure if I should call this loop but is there a way to keep checking if app is minimized and then stop the music, and once app is resumed the music should continue. I am doing this with Intervals inside of NgOnInit

It looks like this

 ngOnInit(){

    setInterval(() => {
      document.addEventListener("pause", () => {
        this.isPlaying = false;
        this.player.stop();
      }, false);

      document.addEventListener("resume", () => {
        this.isPlaying = false;
        this.player.stop();

      }, false);
    })
}

But I am pretty sure this is the wrong way of doing it, I can't do it in another component though because the music stop function is in this one. Can anyone help me do it? This is working btw when I minimize the music is stopped but it does not resume once the app is reopened.

Also how do you loop stuff in Angular I really do not get it, is there not a main function that you can put stuff like this in to check for entire project without having to click buttons?