Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Android support and redesigned the API #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Added Android support and redesigned the API #32

wants to merge 2 commits into from

Conversation

Guichaguri
Copy link

@Guichaguri Guichaguri commented Sep 6, 2016

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
Copy link
Author

Guichaguri commented Sep 6, 2016

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.

@Guichaguri Guichaguri mentioned this pull request Sep 6, 2016
@shi11
Copy link
Owner

shi11 commented Sep 7, 2016

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
Copy link
Author

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

@LukePulverenti
Copy link

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
Copy link
Author

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants