react-native-music-control
React Native module to control remote controls on lockscreen + display Now playing Info on lockscreen (MPNowPlayingInfoCenter)
Play well with React Native Sound
Mix between :
- https://github.com/Muntligt/cordova-plugin-remotecommand (iOS)
- https://github.com/Muntligt/cordova-plugin-nowplaying (iOS)
- https://github.com/homerours/cordova-music-controls-plugin (Android)
- https://github.com/shi11/RemoteControls/pull/32 (Android)
Project using it :
Install
Add it to your project
npm install react-native-music-control --save
iOS
Automatic
react-native link
⚠️ You must enable Audio Background mode in XCode project settings :
Manual
In XCode, right click Libraries. Click Add Files to "[Your project]". Navigate to node_modules/react-native-music-control. Add the file MusicControl.xcodeproj.
In the Project Navigator, select your project. Click the build target. Click Build Phases. Expand Link Binary With Libraries. Click the plus button and add libMusicControl.a under Workspace.
CocoaPods
pod 'react-native-music-control', :path => '../node_modules/react-native-music-control'
Android
Automatic
react-native link
Manual
android/app/build.gradle
dependencies { ... compile "com.facebook.react:react-native:+" // From node_modules+ compile project(':react-native-music-control')}
android/settings.gradle
...include ':app'+include ':react-native-music-control'+project(':react-native-music-control').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-music-control/android')
MainActivity.java
+import com.tanguyantoine.react.MusicControl; public class MainApplication extends Application implements ReactApplication { //...... @Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList(+ new MusicControl(), new MainReactPackage() ); } //...... }
Use
;
Now Playing
This method enables the music controls. To disable them, use resetNowPlaying()
You should call this method after a sound is playing.
For Android's rating system, remove the rating
value for unrated tracks, use boolean for RATING_HEART or RATING_THUMBS_UP_DOWN and use a number for other types. Note: To use custom types, you have to define the type with updatePlayback
before calling this function.
MusicControl
Playback
You don't need to call this method filling all properties, but you should always fill elapsedTime
for iOS support and better performance on Android.
You also don't need to call this method repeatedly to update the elapsedTime
, only call it when you need to update any other property
MusicControl
Examples
// Changes the state to pausedMusicControl // Changes the volumeMusicControl
Reset now playing
Resets and hides the music controls
MusicControl
Stop controls
Resets, hides the music controls and disables everything
MusicControl
Enable/disable controls
iOS: Lockscreen
Android: Notification and external devices (smartwatches, cars)
// Basic ControlsMusicControlMusicControlMusicControlMusicControlMusicControl // SeekingMusicControl // iOS onlyMusicControl // iOS onlyMusicControl // Android onlyMusicControlMusicControl // Android Specific OptionsMusicControlMusicControl // Only affected when remoteVolume is enabledMusicControl // iOS Specific OptionsMusicControlMusicControl
skipBackward
and skipForward
controls on accept additional configuration options with interval
key:
MusicControl)MusicControl)
Important Notes:
- Android only supports the intervals 5, 10, & 30, while iOS supports any number
- The interval value only changes what number displays in the UI, the actual logic to skip forward or backward by a given amount must be implemented in the appropriate callbacks
- When using react-native-sound for audio playback, make sure that on iOS
mixWithOthers
is set tofalse
inSound.setCategory(value, mixWithOthers)
. MusicControl will not work on a real device when this is set totrue
. - For lockscreen controls to appear enabled instead of greyed out, the accompanying listener for each control that you want to display on the lock screen must contain a valid function:
MusicControl.on('play', () => {
// A valid funcion must be present
player.play()
})
There is also a closeNotification
control on Android controls the swipe behavior of the audio playing notification, and accepts additional configuration options with the when
key:
// Always allow user to close notification on swipeMusicControl // Default - Allow user to close notification on swipe when audio is pausedMusicControl // Never allow user to close notification on swipeMusicControl
Register to events
{ MusicControl; // on iOS, pause playback during audio interruptions (incoming calls) and resume afterwards. MusicControl; MusicControl // 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 MusicControl MusicControl MusicControl MusicControl; MusicControl; MusicControl; // Android only (Seconds) MusicControl; // Android only (0 to maxVolume) - Only fired when remoteVolume is enabled // Android Only (Boolean for RATING_HEART or RATING_THUMBS_UP_DOWN, Number for other types) MusicControl; MusicControl; // iOS only MusicControl; // iOS only MusicControl; // iOS only MusicControl; MusicControl; // Android Only MusicControl}
Customization
It is possible to customize the icon used in the notification on Android.
By default you can add a drawable resource to your package with the file name music_control_icon
and the notification will use your custom icon.
If you need to specify a custom icon name, or change your notification icon during runtime, the setNowPlaying
function accepts a string
for an Android drawable resource name in the notificationIcon
prop. Keep in mind that just like with music_control_icon
the resource specified has
to be in the drawable package of your Android app.
MusicControl;
TODOS
- Android support
- Test
- Publish package
- React-Native link configuration for Android
- React-Native link configuration for iOS
- Android : Handle remote events
- Android : Display cover artwork