"[SOLVED] Cant get "running" or "stop" status with onStatusUpdate after "start"

Hello all helpers
With ionic-native media plugin, i just want to remove a message “playing” when the player reachs the end of an audio file . My code doesn’t work, except for the first event “start” that ionic-native send back well. After that, nothing :smile: So how can i get running status or success to handle my message ?

const f:MediaObject = this.media.create(path);

      f.onStatusUpdate.subscribe(status => 
        { this.message=this.message+" > onStatusUpdate="+status; });   // just receive 1=start event
      f.onSuccess.subscribe(() => 
        { this.message=this.message+" > onSuccess"+status; });
      f.onError.subscribe(error => 
        {  this.message=this.message+" > onError"+error; });
              
      f.play();

[SOLVED] by myself. after hard work !

Cordova update and return to ionic-native the status of the player only if you call a function on the player ( see the java code ). So i choose to query a function ( here “duration”, but you can choose the one you want ) with setInterval in order to get the status update in my callback as below : ( more things below code :wink: )

      if (this.mediaTimer !=null) {
        clearInterval(this.mediaTimer);
        this.mediaTimer=null;
      }

    this.memoMedia = this.media.create(path);

    this.memoMedia.onStatusUpdate.subscribe(status => 
        { this.message=this.message+" > onStatusUpdate="+status;

        if (status.toString()=="1") { //player start
    
            let self=this;
            this.mediaTimer = setInterval(function(){  //here the set interval function to refresh status 
                let duration=self.memoMedia.getDuration();
                self.message=self.message+" > duration="+duration;
                            
            }, 500);
        }

        if (status.toString()=="4") { // player end running
            if (this.mediaTimer !=null) {
                //clearInterval(this.mediaTimer);    // (*) don t do clearInterval here, or your ionic will not work, see below
                //TODO here : handle html, remove "playing" message
            }
        }

    }); 

    this.memoMedia.onSuccess.subscribe(() => 
        { 
        this.message=this.message+" > onSuccess complete";
    });

    this.memoMedia.onError.subscribe(error => 
        {
        this.message=this.message+" > onError="+error; 
        //clearInterval(this.mediaTimer);  (*) don t do clearInterval here, or your ionic will not work, see below
    });
        
    
    this.memoMedia.play();

          

By the way, 2 more things to help you :

  • don’t forget that onSuccess and onsStatusUpdate are asynch functions. So if you choose to put the clearInterval function in OnSucess, may be (if statusupdate==4 …) will never be executed if you’re code execute OnSucess with clearInterval. I choose to wait and test the end of running on the line (if statusupdate==4 …) but you can do it in OnSuccess
  • getDuration() : if you re just interrested with duration, don t try to get duration just after the start event statusupdate==1, you will always receive the value -1. The solution : you can do as here, or a simple thing like this : setTimeout(self.getDuration(), 500). It will work. But as setTimeout is running just once, don t except to receive running or stop event.
  • (*) clearInterval : i tried this clearInterval in callBacks and that was such a mess too … So eventually, i clearInterval before creating the media ( see first line of code )

I lost so many hours on this, i really hope this solution will help you. Please reply here, it will be a real pleasure to know that i helped you :smile: