How to stop media plugin audio when navigating away from a page

Hello, I am playing audios in an ionic app with cordova media plugin.

so, when users navigate a way from the audio page, I want the audio to stop because it’s causing an unexpected behaviour that allows users to play multiple audios at the same time thereby causing overlap of audios .

This is what I have tried that did not work

ngOnDestroy(): void {
   
    if(this.Audiofile){
      //this.Audiofile.stop()
     this.Audiofile.release()
      }
   
  }
  ionViewDidLeave(){
   
    if(this.Audiofile){
    //this.Audiofile.stop()
    this.Audiofile.release()
    }
  }
  ionViewWillLeave(){
  
    if(this.Audiofile){
    //this.Audiofile.stop()
    this.Audiofile.release()
    }
  }

Are you using tabs, by any chance?

Thank you, yes I am using tabs

Then you’re going to have to listen for tab changes on the <ion-tabs>. Its children are cached - they don’t get destroyed.

Thank you, any hint on how to go about that ?

onTabChange() {
       
    }

?

hi, i have the same issue. but i am using common pages. not tabs.

how to stop audio from another page?

Facing same issue. have you got any solution?

My general answer to “how do I fiddle with anything involving page B from page A” is “don’t”, in order to preserve separation of concerns.

Therefore, the problem then becomes “how do I manage a resource so that both page A and page B can interact with it?”, and the simplest answer would be to put it in a service that is injected by both pages.