tanguyantoine / react-native-music-control

Display and manage media controls on lock screen and notification center for iOS and Android.
https://www.npmjs.com/package/react-native-music-control
MIT License
697 stars 264 forks source link

PlayPause button on Lightning EarPods doesn't work #396

Closed dwgdev07 closed 2 years ago

dwgdev07 commented 2 years ago

Description

Hi, i have the problem, that the playPause button on EarPods with lightning connector doesn't work - bluetooth earphones work as expected. Here is the hook I use to init the music controls.

import MusicControl, {Command} from 'react-native-music-control';
import {usePlayerStore} from './usePlayerStore';
import {Platform} from 'react-native';

export const useControlCenter = () => {
  const playerStore = usePlayerStore();
  const init = () => {
    MusicControl.enableBackgroundMode(true);

    if (Platform.OS === 'ios') {
      MusicControl.handleAudioInterruptions(true);
    }

    // Basic Controls
    MusicControl.enableControl('play', true);
    MusicControl.enableControl('pause', true);
    MusicControl.enableControl('stop', false);

    MusicControl.on(Command.play, () => {
      playerStore.updatePaused(false);
    });

    // on iOS this event will also be triggered by audio router change events
    // happening when headphones are unplugged or a bluetooth audio peripheral disconnects from the device
    MusicControl.on(Command.pause, () => {
      playerStore.updatePaused(true);
    });

    MusicControl.on(Command.stop, () => {
      playerStore.updatePaused(true);
    });

    MusicControl.on(Command.togglePlayPause, () => {
      playerStore.updatePaused(!playerStore.paused);
    });
  };

  const updatePosition = (position: number) => {
    MusicControl.updatePlayback({
      elapsedTime: position,
    });
  };

  return {
    init: init,
    updatePosition: updatePosition,
  };
};
  1. Platform ?

    • [X] iOS
    • [ ] Android
3. Device - [ ] Simulator - [X] Real device
dwgdev07 commented 2 years ago

Found it. Had to enable togglePlayPause with MusicControl.enableControl('togglePlayPause', true);. Maybe it would be helpful to include this line in the README.md examples.