homerours / cordova-music-controls-plugin

A Cordova plugin displaying music controls in notifications (cordova-plugin-music-controls)
MIT License
155 stars 190 forks source link

Volume Up and Down not firing #72

Open vickynathaiya opened 7 years ago

vickynathaiya commented 7 years ago

Hi, i have tried many way to detect volume up and down but its not working. i don't know why. My code is as below. please let me know where i am wrong.

export class HomePage {

constructor(public navCtrl: NavController, public platform: Platform, private push: Push, public musicControls: MusicControls) {
    platform.ready().then(() => {
        alert("system ready");

    });

    musicControls.subscribe().subscribe(action => {

alert(JSON.stringify(action)); switch(action) { case 'music-controls-next': alert("Next"); break; case 'music-controls-previous': // Do something alert("Previous"); break; case 'music-controls-pause': // Do something alert("Pause"); break; case 'music-controls-play': // Do something alert("Play"); break; case 'music-controls-destroy': // Do something break;

   // Headset events (Android only)
   case 'music-controls-media-button' :
       // Do something
       alert("Media Button");
       break;
    case 'music-controls-media-button-volume-up'
        alert("volume up");
        break;
    case 'music-controls-media-button-volume-down'
        alert("volume up");
        break;
   case 'music-controls-headset-unplugged':
       // Do something
       alert("headset unplugged");
       break;
   case 'music-controls-headset-plugged':
       // Do something
       alert("Headset plugged");
       break;
   default:
       break;

}

});

musicControls.listen(); // activates the observable above

//musicControls.updateIsPlaying(true);

}

}

homerours commented 7 years ago

Hi @vickynathaiya, Are the volume up/down the only events that are not triggered ?

vickynathaiya commented 7 years ago

Hi @homerours yes, actually i am working with headset events. headset plugged, unplugged and headset hook event are working properly. But i don't know why volume up and down not working even with headset and without headset

BuddyLReno commented 7 years ago

The code above looks like it won't work. The action coming back is in JSON format. You'll need to retrieve the action from the message parameter after parsing the JSON.

// action looks like this as a string: '{\"message\":\"music-controls-media-button-volume-down\"}'
const eventType = JSON.parse(action).message

Then can you use your switch statement on eventType.

Sbissa commented 7 years ago

I'm having problems with this too, events from the headphone buttons doesn't fire. Only plug and unplug events are fired.

` function events(action) {

  const message = JSON.parse(action).message;
  switch(message) {
    case 'music-controls-next':
    // Do something
    break;
    case 'music-controls-previous':
    // Do something
    break;
    case 'music-controls-pause':
    // Do something
    break;
    case 'music-controls-play':
    // Do something
    break;
    case 'music-controls-destroy':
    // Do something
    break;

    // Headset events (Android only)
    // All media button events are listed below
    case 'music-controls-media-button' :
    // Do something
    break;
    case 'music-controls-headset-unplugged':
    // Do something
    break;
    case 'music-controls-headset-plugged':
    // Do something
    break;
    default:
    break;
  }
};
MusicControls.subscribe(events);
MusicControls.listen();`
quran-memorizer commented 7 years ago

@Sbissa Your cases are not sufficient to cover play and pause events from headset. Listen to more events.

Something like this.

`

     this._ngZone.run(() => {
    switch(message) {
      case 'music-controls-next':
      case 'music-controls-skip-forward':
        self.onSkipForwardFromOutside();
        // Do something
        break;
      case 'music-controls-previous':
      case 'music-controls-skip-backward':
        self.onSkipBackwardFromOutside();
        // Do something
        break;
      case 'music-controls-pause':
      case 'music-controls-toggle-play-pause' :
      case 'music-controls-play':
        self.onPlayFromOutside();
        break;
      case 'music-controls-seek-to':
        // Do something
        break;
        // Headset events (Android only)
        // All media button events are listed below
      case 'music-controls-media-button-music':
      case 'music-controls-media-button-headset-hook':
      case 'music-controls-media-button-pause':
      case 'music-controls-media-button-play':
      case 'music-controls-media-button-play-pause':
        this.registerTap(()=>{this.onPlayFromOutside()},()=>{this.onRepeatWithoutTouch()},CONFIG.DOUBLE_TAP_DELAY);
        console.log(action);
        break;

      case 'music-controls-media-button-skip-forward':
      case 'music-controls-media-button-step-forward':
      case 'music-controls-media-button-meta-right':
      case 'music-controls-media-button-next':
        self.onSkipForwardFromOutside();
        break;
      case 'music-controls-media-button-skip-backward':
      case 'music-controls-media-button-step-backward':
      case 'music-controls-media-button-meta-left':
      case 'music-controls-media-button-previous':
        self.onSkipBackwardFromOutside();
        break;
      case 'music-controls-headset-unplugged':
        self.pause();
        // Do something
        break;
      case 'music-controls-headset-plugged':
        self.pause();
        // Do something
        break;
      case 'music-controls-media-button-fast-forward':
      case 'music-controls-media-button-rewind':
        break;
      default:
        break;
    }
  });`
Sbissa commented 7 years ago

@quran-memorizer the problem isn't the switch. I'm not receiving any events, only the plug and unplug. The only message i've got from the subscribe is the plug and unplug when i press the buttons from the headset nothing happens

ghenry22 commented 6 years ago

@Sbissa - this should be resolved in the latest release, try installing from the github repo directly as NPM is out of date currently.

BuddyLReno commented 6 years ago

Yeah, currently @homerours is the only one who has access to publish the pacakge to NPM.

Sbissa commented 6 years ago

Thanks guys, i used the github repo and works now!