shi11 / RemoteControls

cordova 3 plugin for interacting with ios7 remote controls and metadata
57 stars 41 forks source link

Added Android support and redesigned the API #32

Open Guichaguri opened 8 years ago

Guichaguri commented 8 years ago

Here it is the Android Support with the new API.

Lock screen Notification

This implementation works with Android 4.X and greater. Most of the properties are not visible, but they are for integration with smart watches, cars, etc.

The lock screen wallpaper only changes in Android 5.X and greater.

API Functions supported by Android:

// All properties in updateActions are optional and defaults to false
window.remoteControls.updateActions({
    play: true,
    pause: true,
    stop: false,
    seekTo: false,
    skipToNext: true,
    skipToPrevious: true,
    rate: false,
    volume: true
}, successCallback, errorCallback);

window.remoteControls.updateMetadata({
    title: 'Prime Time of Your Life', // Required (String)
    artist: 'Daft Punk', // Required (String)
    album: 'Human After All', // Optional (String)
    cover: 'http://i.imgur.com/HDDlamS.jpg', // Optional (URL/Path)
    description: '', // Optional (String)
    color: 0xd85656, // Optional (Color Number from 0x000000 to 0xFFFFFF)
    genre: 'Industrial Rock, Noise Rock', // Optional (String)
    date: '2006-06-17T00:00:00Z', // Optional (String)
    rating: 77, // Optional (Percentage from 0 to 100)
    duration: 239 // Optional (Number)
}, successCallback, errorCallback);

window.remoteControls.updatePlayback({
    state: 0, // Required (-1: Error, 0: Stopped, 1: Playing, 2: Paused, 3: Buffering)
    volume: 100, // Optional (Percentage from 0 to 100)
    speed: 1, // Optional (Number. 1: Normal speed)
    elapsedTime: 103, // Optional (Number from 0 to duration)
    bufferedTime: 200 // Optional (Number from 0 to duration)
}, successCallback, errorCallback);

window.addEventListener("remotecontrols", function(event) {
    switch(event.action) {
        case 'play':
        case 'pause':
        case 'stop':
        case 'next':
        case 'previous':
            // Handle regular buttons
            break;
        case 'volume':
            // Change the volume
            console.log(event.volume); // Percentage
            break;
        case 'seek':
            // Change the playback position
            console.log(event.position); // A number from 0 to "duration"
            break;
        case 'rate':
            // Change the rating
            console.log(event.rating); // Percentage
            break;
    }
});
Guichaguri commented 8 years ago

For iOS development, you should know that the new event handler is the same from cordova-plugin-battery-status, since the sendJavascript function is now deprecated.

Since using OS icons is not recommended as they can be removed or renamed, I've included five icons from Material Icons, which are Apache 2.0 licensed, but I can change back to OS icons if you don't want to include assets.

And also, window.remoteControls.updateMetadata(null, successCallback, errorCallback); disables the notification and the media buttons

The code is a little messy, but it works. Just let me know if it needs any API changes for iOS.

shi11 commented 8 years ago

Sweet @Guichaguri! From my initial read through, it looks good. There will need to be an update to the Obj-C updateMetadata function to handle the object that's passed in rather than the array thats currently used. Hopefully I can put a little time towards it tomorrow. Thanks again for picking up the ball and running with it.

Guichaguri commented 8 years ago

I just noticed the java files were outside the src folder. Fixed it, should work as expected now

LukePulverenti commented 7 years ago

Does this work with the app in the background for an extended period of time? What happens if the cordova activity is paused while the app is in the background? Most apps nowadays play media from a background service, and then use the service for notification interaction, thereby decoupling it from the activity.

Guichaguri commented 7 years ago

@LukePulverenti I can't remember how I made it, I also don't use Cordova anymore. I've been using React Native as it has much better performance than Cordova, I've made the whole Android side for the react-native-music-control and now I'm working on a module combining audio, media controls and chromecast, the way it should have been since the start