Spotify API disable next / previous controls - spotify

Is there a way to disable next / previous track controls in the player with the new API, like Soundrop does?
I have come across such questions, there are a couple of suggestions like playing a single track or playing a context with only one track in it, or using setContextCanSkipPrev methods on the player, but none work for the new API. I need a solution for the API version 1.0.0

You need to create a temporary playlist with a single track and use the enforceRules function:
require(['$api/models'], function(models) {
var tempName = 'temp' + (new Date()).getTime();
models.Playlist.createTemporary(tempName).done(function(playlist) {
playlist.enforceRules('stream');
playlist.load("tracks").done(function(loadedPlaylist) {
var track = models.Track.fromURI('spotify:track:7B1Dl3tXqySkB8OPEwVvSu');
loadedPlaylist.tracks.add(track);
models.player.playContext(loadedPlaylist, 0);
});
});
});
At the moment, it seems the documentation for the API is missing the description of this function.

Related

How to get room's clients list in socket.io 1.0

I can get room's clients list with this code in socket.io 0.9.
io.sockets.clients(roomName)
How can I do this in socket.io 1.0?
Consider this rather more complete answer linked in a comment above on the question: https://stackoverflow.com/a/24425207/1449799
The clients in a room can be found at
io.nsps[yourNamespace].adapter.rooms[roomName]
This is an associative array with keys that are socket ids. In our case, we wanted to know the number of clients in a room, so we did Object.keys(io.nsps[yourNamespace].adapter.rooms[roomName]).length
In case you haven't seen/used namespaces (like this guy[me]), you can learn about them here http://socket.io/docs/rooms-and-namespaces/ (importantly: the default namespace is '/')
Updated (esp. for #Zettam):
checkout this repo to see this working: https://github.com/thegreatmichael/socket-io-clients
Using #ryan_Hdot link, I made a small temporary function in my code, which avoids maintaining a patch. Here it is :
function getClient(roomId) {
var res = [],
room = io.sockets.adapter.rooms[roomId];
if (room) {
for (var id in room) {
res.push(io.sockets.adapter.nsp.connected[id]);
}
}
return res;
}
If using a namespace :
function getClient (ns, id) {
return io.nsps[ns].adapter.rooms[id]
}
Which I use as a temporary fix for io.sockets.clients(roomId) which becomes findClientsSocketByRoomId(roomId).
EDIT :
Most of the time it is worth considering avoiding using this method if possible.
What I do now is that I usually put a client in it's own room (ie. in a room whose name is it's clientID). I found the code more readable that way, and I don't have to rely on this workaround anymore.
Also, I haven't tested this with a Redis adapter.
If you have to, also see this related question if you are using namespaces.
For those of you using namespaces I made a function too that can handle different namespaces. It's quite the same as the answer of nha.
function get_users_by_room(nsp, room) {
var users = []
for (var id in io.of(nsp).adapter.rooms[room]) {
users.push(io.of(nsp).adapter.nsp.connected[id]);
};
return users;
};
As of at least 1.4.5 nha’s method doesn’t work anymore either, and there is still no public api for getting clients in a room. Here is what works for me.
io.sockets.adapter.rooms[roomId] returns an object that has two properties, sockets, and length. The first is another object that has socketId’s for keys, and boolean’s as the values:
Room {
sockets:
{ '/#vQh0q0gVKgtLGIQGAAAB': true,
'/#p9Z7l6UeYwhBQkdoAAAD': true },
length: 2 }
So my code to get clients looks like this:
var sioRoom = io.sockets.adapter.rooms[roomId];
if( sioRoom ) {
Object.keys(sioRoom.sockets).forEach( function(socketId){
console.log("sioRoom client socket Id: " + socketId );
});
}
You can see this github pull request for discussion on the topic, however, it seems as though that functionality has been stripped from the 1.0 pre release candidate for SocketIO.

Spotify API 1.0 -- no addEventListener method exposed for player object

The Spotify API docs suggest that the player class exposes an addEventListener method:
models.player.addEventListener('change', function() { … });
I am getting undefined when trying to reference this method:
var sp = getSpotifyApi(1);
var models = sp.require("$api/models");
console.log(models.player);
console.log(models.player.addEventListener);
The second log message returns 'undefined'
Any ideas what I'm doing wrong here?
Thanks,
Randy
I'm a bit of a Javascript noob, but I have successfully connected to addEventListener using the recommended format for require. I don't think you're supposed to call getSpotifyApi any more.
require(['$api/models'], function(models) {
...
});
https://developer.spotify.com/technologies/apps/upgrade-guide/1.0.0
The code you are writing uses the API 0.x, that is why models.player.addEventListener is not defined. You need to use the more recent version of the API, 1.x.
If you want to detect a change in the Player you need to do:
require(['$api/models'], function(models) {
models.player.addEventListener('change', function(p) {
// p.data.track contains the current track
});
});
You have an example of that in the "Current track" example in the Apps Tutorial.

Updating $scope after $HTTP.post

new at this.
I have 2 div's;
When I use one to post new value into the server, the post action works fine.
How do ensure this post updates the scope in the other div? (to update the div with the new entry using ng-repeat)
The HTTP.post action seems to be working as the data appears on refresh.
I have tried $scope.apply() but it doesn't appear to be working.
// this is the controller to get the data from the server
app.controller('projectsController',
function projectsController($scope, projectsFactory){
$scope.projects = projectsFactory.query();
});
and the factory to get the data from server
app.factory('projectsFactory',
function($resource,localStorage, $rootScope, $http){
return $resource('/api/project:id', {id: '#id'});
});
This is the controller to take the user input and post to back to the server
app.controller('projectEditController', function($scope, projectEdit){
$scope.projectEdit = projectEdit;
});
And the service to post the data to the server
app.service('projectEdit',
function projectEdit(localStorage,$rootScope, $http) {
var self = this;
self.add = function(newProject,projects) {
newProject = angular.copy(newProject);
var newProject = (JSON.stringify(newProject));
return $http.post('/api/project', newProject )
.then(function(response) {
newProject.id = response.data.id;
});
};
Which seems to be fine as it posts the data to the Sever fine.
What I am trying to figure out is how to get the
"$scope.projects = projectsFactory.query();" to update once the post operation is complete.
this will update the display in the HTML, which is the goal.
Hopefully this is an easy solve for a good developer, but I am stumped!
Thanks for your time.
Conor
Create 1 wrapper div, and set your controller to it with ng-controller.
Put your 2 divs in the wrapper div. Now the scope created by your controller is available in both divs, this is the easy way. When something updates data in that scope, and you have other things in the dom referencing that variable, then the dom will refresh like you want.
If you really want different controllers for each child div, that's fine, just be careful around inherited scopes. It really is much easier to just have one controller in a situation like this.
By the way, I saw that you mentioned scope.apply(). Be careful, apply() is a real javascript function, and NOT the one you want. You should call scope.$apply(), $apply is the angular function, and to avoid confusion, you could call scope.$digest instead.

Retrieving album/playlist information API

I read in another thread (http://stackoverflow.com/questions/9474011/showing-a-album-cover)
that the:
Please don't use any of the sp. APIs - they're private and going away soon.
My question is, what is the correct way of getting album and/or playlist information from the API?
I'm currently playing around with this:
sp.core.getMetadata(uri, {
onSuccess: function(uri) {
// Success
},
onFailure: function() {
// Failure
}
});
I guess this is private and shouldn't be used right? Instead I should get the info from the models.* object? If not, is there another preferred method of dealing with this?
Always use models. Documentation can be found here.
For example:
var sp = getSpotifyApi(1);
var models = sp.require('sp://import/scripts/api/models');
var a = models.Album.fromURI("spotify:album:5zyS3GEyL1FmDWgVXxUvj7", function(album) {
console.log("Album loaded", album.name);
});

Playlist null value return

I'm still developping a Spotify App, but I thinkg there is some bug in the API and they report it as a bug for me.
I make then the test with the tutorial app using the function "Get songs from a playlist URL", they are in the tutorial using also the callback function like this
var pl = models.Playlist.fromURI(playlist_url, function(playlist)
But even using the Callback function on first load there is some null value.
With my own playlist, I don't have the issue but with playlist from other users I got the issue.
Does anybody else get the issue ? Is there a way to report API bug ?
For those who want to test my playlist: http://open.spotify.com/user/gpsnail/playlist/6qhk1FhYKwyanNAu91GftW
The Spotify Apps API 0.X is not longer supported. It might be the case that there was a bug and the data was being rendered before it was actually fetched. I would recommend you to use the newer version of the API, in which you can fetch the contents of a playlist doing:
require(['$api/models'], function(models) {
var playlistURI = 'spotify:user:gpsnail:playlist:6qhk1FhYKwyanNAu91GftW';
models.Playlist.fromURI(playlistURI)
.load('tracks')
.done(function(p) {
p.tracks.snapshot()
.done(
function(snapshot) {
for (var i = 0, l = snapshot.length; i < l; i++) {
var track = snapshot.get(i);
// we print out some basic data from the track
console.log(track.uri, track.name);
}
})
.fail(
function(){
console.error('Error retrieving snapshot');
});
})
.fail(function(){
console.error('Error retrieving playlist information');
});
});
There is more information about how to upgrade to the upgrade guide.

Resources