Created
December 1, 2018 18:13
-
-
Save BrychanOdlum/8e18651cce5cc71bbb630ae77b1f5121 to your computer and use it in GitHub Desktop.
MusicKit-example.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener('musickitloaded', function() { | |
// MusicKit global is now defined | |
MusicKit.configure({ | |
developerToken: '**JWT_TOKEN**', | |
app: { | |
name: 'Apple Musish', | |
build: '0.01a' | |
} | |
}); | |
var music = MusicKit.getInstance(); | |
// You can wrap any method to ensure authorization before calling: | |
music.authorize().then(function() { | |
music.api.library.albums().then(function(someData) { | |
console.log(someData); | |
someData.forEach(function(album) { | |
const bodyContents = "<h1>" + album.attributes.name + "</h1><h2>" + album.attributes.artistName + "</h2>" | |
document.body.innerHTML += '<div style="width:200px; height:50px;background:#ccc;" class="album">' + bodyContents + '</div>'; | |
}); | |
}).catch(function(error) { | |
console.error(error); | |
}); | |
}); | |
}); | |
function doPlaySomeHamiltion() { | |
var music = MusicKit.getInstance(); | |
music.authorize().then(function() { | |
var url = 'https://itunes.apple.com/us/album/hamilton-original-broadway-cast-recording/1025210938'; | |
music.setQueue({ url: url }).then(function(queue) { | |
// Queue is instantiated and set on music player. | |
console.log(queue); | |
music.player.prepareToPlay(queue.nextPlayableItem).then(function() { | |
music.player.play(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment