Browser-side JS library for controlling Google Music
JavaScript
56
430 commits
updated Dec 5, 2022
Browser-side JS library for controlling Google Music.
This was built as part of google-music-webkit, a node-webkit wrapper around Google Music. It was forked from radiant-player-mac@v1.3.1, developed and created by Sajid Anwar and James Fator to make it reusable and well tested.
It is now being maintained by the teams of GPMDP and Radiant Player
gmusic.js is not created by, affiliated with, or supported by Google Inc.
playback.getPlaybackTime renamed to playback.getCurrentTimeplayback.setPlaybackTime renamed to playback.setCurrentTimeplayback.getSongInfo renamed to playback.getCurrentTrackextras.getSongURL renamed to extras.getTrackURLchange:song renamed to change:trackalbumArt prop instead of artgetTotalTimeisPlayingThe method toggleRepeat() no longer accepts arguments and setRepeat(mode) has replaced its functionality.
On Thursday May 14, 2015 Google launched a Material redesign of the site. This broke a lot of selectors/functionality. In 3.0.0, we updated our integration to handle those changes. The developer-facing interface has not changed but the underlying system was a breaking change so we decided to make it a major release.
Install the module with: npm install gmusic.js
// Load and initialize GMusic
var GMusic = require('gmusic.js');
window.gmusic = new GMusic(window);
// Access volume
window.gmusic.volume.getVolume(); // 50 (ranges from 0 to 100)
gmusic.js exposes a constructor, GMusic as its module.exports (window.GMusic for bower/vanilla).
new GMusic()Constructor for a new Google Music API
gmusic.volume exposes interfaces to the volume controls of Google Music. Volume can range from 0 to 100 in steps of 5 (e.g. 10, 15, 20).
volume.getVolume()Retrieve the current volume setting
Returns:
Number - Integer from 0 to 100 representing volumevolume.setVolume(vol)Change the volume setting
Number - Integer to set volume tovolume.increaseVolume(amount)Raise the volume by an amount
Number - Optional number to raise volume by
volume.decreaseVolume(amount)Lower the volume by an amount
Number - Optional number to lower volume by
gmusic.playback exposes interfaces to the state of music playback and its behavior (e.g. shuffle).
playback.getCurrentTime()Retrieve the current progress in a track
Returns:
Number - Integer representing milliseconds from the start of the trackplayback.setCurrentTime(milliseconds)Jump the current track to a time
Number - Integer representing milliseconds to jump the current track toplayback.getTotalTime()Retrieve the length of the current track
Returns:
Number - Integer representing the length of the track in millisecondsplayback.isPlaying()Determine if a track is current playing
Returns:
Boolean - True if the a track is currently playing, else falseplayback.getCurrentTrack()Retrieve current track's metadata
Returns:
Track - Container for track infoplayback.playPause()Toggle between play and pause for the current track
This will not work if there are no tracks in the queue.
playback.getPlaybackState()Returns:
Number - Current status of music playback (e.g. 0, 1, 2)
GMusic.PlaybackStatus.STOPPED, GMusic.PlaybackStatus.PAUSED, and GMusic.PlaybackStatus.PLAYINGplayback.forward()Move to the next track
playback.rewind()Move to the previous
playback.getShuffle()Retrieve the status of shuffle
Returns:
String - Current state of shuffle (e.g. ALL_SHUFFLE, NO_SHUFFLE)
ALL_SHUFFLE will shuffle between all tracksNO_SHUFFLE will play the tracks in the order they were addedGMusic.ShuffleStatus.ALL_SHUFFLE or GMusic.ShuffleStatus.NO_SHUFFLEplayback.setShuffle(mode)Set the shuffle mode
String - Value to change shuffle to
ALL_SHUFFLE and NO_SHUFFLEplayback.toggleShuffle()Toggle to between shuffle being active or inactive
playback.getRepeat()Retrieve the current setting for repeat
Returns:
String - Current setting for repeat (e.g. LIST_REPEAT, SINGLE_REPEAT, NO_REPEAT)
LIST_REPEAT will repeat the queue when it reaches the last trackSINGLE_REPEAT will repeat the current track indefinitelyNO_REPEAT will not repeat the queueGMusic.RepeatStatus.LIST_REPEAT, GMusic.RepeatStatus.SINGLE_REPEAT, GMusic.RepeatStatus.NO_REPEATplayback.setRepeat(mode)Change the current setting for repeat
String - Value to change repeat to
NO_REPEAT, LIST_REPEAT, SINGLE_REPEAT
playback.getRepeat() for meaningplayback.toggleRepeat()Toggle through the modes for repeat.
NO_REPEAT, LIST_REPEAT, SINGLE_REPEATplayback.isPodcast()Retrieve if the current track is a podcast.
Boolean - True indicates the current track is a podcast.playback.forwardThirty() PODCASTS ONLYMoves the current track position thirty seconds ahead.
playback.rewindTen() PODCASTS ONLYMoves the current track position 10 seconds back.
playback.toggleVisualization()Trigger a visualization for the track. This is typically album art.
This is an untested method.
gmusic.rating exposes interfaces to the rating the current track.
rating.getRating()Retrieve the rating for the current track.
Returns:
String - Rating for current track. This varies from 0 to 5
rating.toggleThumbsUp()Switch between thumbs up and no thumbs up for the current track. If thumbs down was set, this will remove the thumbs down rating.
rating.toggleThumbsDown()Switch between thumbs down and no thumbs down for the current track. If thumbs up was set, this will remove the thumbs up rating.
rating.setRating(rating)Set the rating for the current track
String - Rating to set for the current track. This should be between 1 and 5rating.resetRating()Removes existing rating from the current track
gmusic.extras is a collection of utility functions for Google Music
extras.getTrackURL()Retrieve the URL of the current track for sharing
This is an untested method
Returns:
String - URL for current trackHooks are currently bound via .on and other corresponding methods for node's EventEmitter
gmusic.on('change:track', function (track) {
});
.on('change:track')Triggers when a track changes
gmusic.on('change:track', function (track) {
});
Track - Same as return value of playback.getCurrentTrack().on('change:shuffle')Triggers when shuffle is toggled
gmusic.on('change:shuffle', function (mode) {
});
String - Mode that shuffle changed to
playback.getShuffle().on('change:repeat')Triggers when repeat is toggled
gmusic.on('change:repeat', function (mode) {
});
String - Mode that repeat changed to
playback.getRepeat().on('change:playback')Triggers when a track is started, paused, or stopped
gmusic.on('change:playback', function (mode) {
});
String - Same as return value of playback.getPlaybackState().on('change:playback-time')Triggers when playback shifts
gmusic.on('change:playback-time', function (playbackInfo) {
});
Object - Container for playback info
Number - Milliseconds of how far a track has progressedNumber - Milliseconds of how long a track is.on('change:rating')Triggers when the current track is rated
gmusic.on('change:rating', function (rating) {
});
Number - Rating the current track changed to
rating.getRating().on('change:podcast')Triggers when switching between listening to music and podcasts
gmusic.on('change:podcast', function (isPodcast) {
});
Boolean - Whether a podcast is currently being played
playback.isPodcast()In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via npm run lint and test via npm test.
Currently, we require a personal Google account exclusively for testing. We will be rating tracks, changing repeat settings, and need predictable track titles. We are using the following tracks (at least 3 required):
Credentials: musopen@mt2014.com / password
https://musopen.org/music/1333/wolfgang-amadeus-mozart/the-marriage-of-figaro-k-492/
Music cannot be uploaded via webdriver instance nor incognito window
If you don't want to contaminate your personal account, create a new user profile in Chrome.
For exactly one track, set the following via "Edit Info" in Google Music:
Name:
this-is-a-name
Artist:
this-is-an-artist
Album Artist:
this-is-an-album-artist
Album:
this-is-an-album
Composer:
this-is-a-composer
Genre:
this-is-a-genre
Year:
2000
Track #:
1 of 10
Disc #:
3 of 5
Explicit:
Unchecked
Once your Google account is registered and the music is uploaded, set the following environment variables
Once your Google account is registered and the music is uploaded, we are ready to run our test suite:
# Set up Google Music credentials as environment variables
export GOOGLE_MUSIC_JS_EMAIL="your.google@account.email"
export GOOGLE_MUSIC_JS_PASSWORD="your-google-account-password"
# If you are on Windows, use the following
# set GOOGLE_MUSIC_JS_EMAIL="your.google@account.email"
# set GOOGLE_MUSIC_JS_PASSWORD="your-google-account-password"
# Install Selenium server files
npm run webdriver-manager-update
# Start up a Selenium server
npm run webdriver-manager-start &
# Run our tests
npm test
# If you are on Windows, please use
# npm run test-windows
We have an automatic build setup on Travis CI that will run every day to catch changes made by Google as quick as possible. These tests make use of BrowserStack's services to remotely run Selenium tests, huge thanks to BrowserStack for the support of Open Source projects like this one!
Nightly builds are triggered on Travis CI by a third party service service Nightli.es
Finnicky tests can be debugged by a few methods
describe.only and it.only to limit test suite to only run 1 at a time# Enter into the node REPL
node
// Prevent node from exiting via anything but an interrupt
process.exit = function () {};
// Emulate mocha being invoked normally
process.argv = ['node', '_mocha', '--timeout', '10000'];
require('mocha/bin/_mocha');
// Our test suite has written `global.browser` as the last Selenium session
global.browser;
browser;
All files were originally licensed at 5ccfa7b3c7bc5231284f8e42c6a2f2e7fe1e1532 under the MIT license. This can be viewed its LICENSE.md. It has been renamed to LICENSE-MIT for ease of disambiguity.
After this commit, all alterations made by Todd Wolfson and future contributors are released to the Public Domain under the UNLICENSE.
JavaScript
100.0%
Browser-side JS library for controlling Google Music
JavaScript
56
430 commits
updated Dec 5, 2022
Browser-side JS library for controlling Google Music.
This was built as part of google-music-webkit, a node-webkit wrapper around Google Music. It was forked from radiant-player-mac@v1.3.1, developed and created by Sajid Anwar and James Fator to make it reusable and well tested.
It is now being maintained by the teams of GPMDP and Radiant Player
gmusic.js is not created by, affiliated with, or supported by Google Inc.
playback.getPlaybackTime renamed to playback.getCurrentTimeplayback.setPlaybackTime renamed to playback.setCurrentTimeplayback.getSongInfo renamed to playback.getCurrentTrackextras.getSongURL renamed to extras.getTrackURLchange:song renamed to change:trackalbumArt prop instead of artgetTotalTimeisPlayingThe method toggleRepeat() no longer accepts arguments and setRepeat(mode) has replaced its functionality.
On Thursday May 14, 2015 Google launched a Material redesign of the site. This broke a lot of selectors/functionality. In 3.0.0, we updated our integration to handle those changes. The developer-facing interface has not changed but the underlying system was a breaking change so we decided to make it a major release.
Install the module with: npm install gmusic.js
// Load and initialize GMusic
var GMusic = require('gmusic.js');
window.gmusic = new GMusic(window);
// Access volume
window.gmusic.volume.getVolume(); // 50 (ranges from 0 to 100)
gmusic.js exposes a constructor, GMusic as its module.exports (window.GMusic for bower/vanilla).
new GMusic()Constructor for a new Google Music API
gmusic.volume exposes interfaces to the volume controls of Google Music. Volume can range from 0 to 100 in steps of 5 (e.g. 10, 15, 20).
volume.getVolume()Retrieve the current volume setting
Returns:
Number - Integer from 0 to 100 representing volumevolume.setVolume(vol)Change the volume setting
Number - Integer to set volume tovolume.increaseVolume(amount)Raise the volume by an amount
Number - Optional number to raise volume by
volume.decreaseVolume(amount)Lower the volume by an amount
Number - Optional number to lower volume by
gmusic.playback exposes interfaces to the state of music playback and its behavior (e.g. shuffle).
playback.getCurrentTime()Retrieve the current progress in a track
Returns:
Number - Integer representing milliseconds from the start of the trackplayback.setCurrentTime(milliseconds)Jump the current track to a time
Number - Integer representing milliseconds to jump the current track toplayback.getTotalTime()Retrieve the length of the current track
Returns:
Number - Integer representing the length of the track in millisecondsplayback.isPlaying()Determine if a track is current playing
Returns:
Boolean - True if the a track is currently playing, else falseplayback.getCurrentTrack()Retrieve current track's metadata
Returns:
Track - Container for track infoplayback.playPause()Toggle between play and pause for the current track
This will not work if there are no tracks in the queue.
playback.getPlaybackState()Returns:
Number - Current status of music playback (e.g. 0, 1, 2)
GMusic.PlaybackStatus.STOPPED, GMusic.PlaybackStatus.PAUSED, and GMusic.PlaybackStatus.PLAYINGplayback.forward()Move to the next track
playback.rewind()Move to the previous
playback.getShuffle()Retrieve the status of shuffle
Returns:
String - Current state of shuffle (e.g. ALL_SHUFFLE, NO_SHUFFLE)
ALL_SHUFFLE will shuffle between all tracksNO_SHUFFLE will play the tracks in the order they were addedGMusic.ShuffleStatus.ALL_SHUFFLE or GMusic.ShuffleStatus.NO_SHUFFLEplayback.setShuffle(mode)Set the shuffle mode
String - Value to change shuffle to
ALL_SHUFFLE and NO_SHUFFLEplayback.toggleShuffle()Toggle to between shuffle being active or inactive
playback.getRepeat()Retrieve the current setting for repeat
Returns:
String - Current setting for repeat (e.g. LIST_REPEAT, SINGLE_REPEAT, NO_REPEAT)
LIST_REPEAT will repeat the queue when it reaches the last trackSINGLE_REPEAT will repeat the current track indefinitelyNO_REPEAT will not repeat the queueGMusic.RepeatStatus.LIST_REPEAT, GMusic.RepeatStatus.SINGLE_REPEAT, GMusic.RepeatStatus.NO_REPEATplayback.setRepeat(mode)Change the current setting for repeat
String - Value to change repeat to
NO_REPEAT, LIST_REPEAT, SINGLE_REPEAT
playback.getRepeat() for meaningplayback.toggleRepeat()Toggle through the modes for repeat.
NO_REPEAT, LIST_REPEAT, SINGLE_REPEATplayback.isPodcast()Retrieve if the current track is a podcast.
Boolean - True indicates the current track is a podcast.playback.forwardThirty() PODCASTS ONLYMoves the current track position thirty seconds ahead.
playback.rewindTen() PODCASTS ONLYMoves the current track position 10 seconds back.
playback.toggleVisualization()Trigger a visualization for the track. This is typically album art.
This is an untested method.
gmusic.rating exposes interfaces to the rating the current track.
rating.getRating()Retrieve the rating for the current track.
Returns:
String - Rating for current track. This varies from 0 to 5
rating.toggleThumbsUp()Switch between thumbs up and no thumbs up for the current track. If thumbs down was set, this will remove the thumbs down rating.
rating.toggleThumbsDown()Switch between thumbs down and no thumbs down for the current track. If thumbs up was set, this will remove the thumbs up rating.
rating.setRating(rating)Set the rating for the current track
String - Rating to set for the current track. This should be between 1 and 5rating.resetRating()Removes existing rating from the current track
gmusic.extras is a collection of utility functions for Google Music
extras.getTrackURL()Retrieve the URL of the current track for sharing
This is an untested method
Returns:
String - URL for current trackHooks are currently bound via .on and other corresponding methods for node's EventEmitter
gmusic.on('change:track', function (track) {
});
.on('change:track')Triggers when a track changes
gmusic.on('change:track', function (track) {
});
Track - Same as return value of playback.getCurrentTrack().on('change:shuffle')Triggers when shuffle is toggled
gmusic.on('change:shuffle', function (mode) {
});
String - Mode that shuffle changed to
playback.getShuffle().on('change:repeat')Triggers when repeat is toggled
gmusic.on('change:repeat', function (mode) {
});
String - Mode that repeat changed to
playback.getRepeat().on('change:playback')Triggers when a track is started, paused, or stopped
gmusic.on('change:playback', function (mode) {
});
String - Same as return value of playback.getPlaybackState().on('change:playback-time')Triggers when playback shifts
gmusic.on('change:playback-time', function (playbackInfo) {
});
Object - Container for playback info
Number - Milliseconds of how far a track has progressedNumber - Milliseconds of how long a track is.on('change:rating')Triggers when the current track is rated
gmusic.on('change:rating', function (rating) {
});
Number - Rating the current track changed to
rating.getRating().on('change:podcast')Triggers when switching between listening to music and podcasts
gmusic.on('change:podcast', function (isPodcast) {
});
Boolean - Whether a podcast is currently being played
playback.isPodcast()In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via npm run lint and test via npm test.
Currently, we require a personal Google account exclusively for testing. We will be rating tracks, changing repeat settings, and need predictable track titles. We are using the following tracks (at least 3 required):
Credentials: musopen@mt2014.com / password
https://musopen.org/music/1333/wolfgang-amadeus-mozart/the-marriage-of-figaro-k-492/
Music cannot be uploaded via webdriver instance nor incognito window
If you don't want to contaminate your personal account, create a new user profile in Chrome.
For exactly one track, set the following via "Edit Info" in Google Music:
Name:
this-is-a-name
Artist:
this-is-an-artist
Album Artist:
this-is-an-album-artist
Album:
this-is-an-album
Composer:
this-is-a-composer
Genre:
this-is-a-genre
Year:
2000
Track #:
1 of 10
Disc #:
3 of 5
Explicit:
Unchecked
Once your Google account is registered and the music is uploaded, set the following environment variables
Once your Google account is registered and the music is uploaded, we are ready to run our test suite:
# Set up Google Music credentials as environment variables
export GOOGLE_MUSIC_JS_EMAIL="your.google@account.email"
export GOOGLE_MUSIC_JS_PASSWORD="your-google-account-password"
# If you are on Windows, use the following
# set GOOGLE_MUSIC_JS_EMAIL="your.google@account.email"
# set GOOGLE_MUSIC_JS_PASSWORD="your-google-account-password"
# Install Selenium server files
npm run webdriver-manager-update
# Start up a Selenium server
npm run webdriver-manager-start &
# Run our tests
npm test
# If you are on Windows, please use
# npm run test-windows
We have an automatic build setup on Travis CI that will run every day to catch changes made by Google as quick as possible. These tests make use of BrowserStack's services to remotely run Selenium tests, huge thanks to BrowserStack for the support of Open Source projects like this one!
Nightly builds are triggered on Travis CI by a third party service service Nightli.es
Finnicky tests can be debugged by a few methods
describe.only and it.only to limit test suite to only run 1 at a time# Enter into the node REPL
node
// Prevent node from exiting via anything but an interrupt
process.exit = function () {};
// Emulate mocha being invoked normally
process.argv = ['node', '_mocha', '--timeout', '10000'];
require('mocha/bin/_mocha');
// Our test suite has written `global.browser` as the last Selenium session
global.browser;
browser;
All files were originally licensed at 5ccfa7b3c7bc5231284f8e42c6a2f2e7fe1e1532 under the MIT license. This can be viewed its LICENSE.md. It has been renamed to LICENSE-MIT for ease of disambiguity.
After this commit, all alterations made by Todd Wolfson and future contributors are released to the Public Domain under the UNLICENSE.
JavaScript
100.0%