Add a song to the current play queue in a Spotify App - spotify

How can I add a track to the current play queue in a Spotify app?

You need to create an unnamed playlist to create your own play queue.
function playTracks(tracks, index) {
var pl = new models.Playlist();
for (var i = 0; i < tracks.length; ++i) {
pl.add(tracks[i]);
}
models.player.play(pl.uri, pl.uri, index);
}

The current play queue seems to be unavailable. But this snippet may be useful if your purpose is to build a queue...
// Create a name for a temporary playlist.
function temporaryName() {
return (Date.now() * Math.random()).toFixed();
}
function getTemporaryPlaylist() {
var temporaryPlaylist = sp.core.getTemporaryPlaylist(temporaryName());
sp.trackPlayer.setContextCanSkipPrev(temporaryPlaylist.uri, false);
sp.trackPlayer.setContextCanRepeat(temporaryPlaylist.uri, false);
sp.trackPlayer.setContextCanShuffle(temporaryPlaylist.uri, false);
return temporaryPlaylist;
}
var tpl = getTemporaryPlaylist();
tpl.add(trackUri);
tpl.add(track2Uri);
//...
sp.trackPlayer.playTrackFromContext(tpl.uri, 0, "", {
onSuccess: //...
onError: //...
onComplete: //...
});

Nothing in the Apps API reference suggests that it is possible. There is no mention of how to do this in any of the apps in the preview build either. The conclusion has to be that doing this is not currently supported.

Related

How to stop re-buffering when rewind or fast forward with AVPlay API on Tizen?

I am building an application for Tizen OS that has an archive playback functionality and now I want to fastForward or Rewind the video but after clicking the button it always starts buffering. This is bad for user experience, so I want to somehow eliminate it. How can I do it?
Here is the code for rewind:
var successCallback = function () {
console.log("Media seek successful");
};
var errorCallback = function () {
console.log("Media seek failed");
};
var currentTime = webapis.avplay.getCurrentTime();
var newTime = currentTime - length;
webapis.avplay.seekTo(newTime, successCallback, errorCallback);
I also tried to add the code to set the buffer size but it doesn't seem to work:
webapis.avplay.setBufferingParam(
"PLAYER_BUFFER_FOR_PLAY",
"PLAYER_BUFFER_SIZE_IN_SECOND",
60);

Node.js dialling back API requests

I have a list of 125,000 + Id numbers.
I am making a request to an api to get more information for each one.
But my problem is that the api will stop giving me a response if I request more then 6 per second.
I need a way to control the speed of the requests.
Just use a function called by setInterval to do the actual API querying ?
Simple example:
var ids = [ /* big array here */ ];
function queryAllIds(ids, callback) {
var processedIds = [];
var lastProcessedId = 0;
var processedIdCount = 0;
var intervalId;
function queryApi() {
var idToProcess = lastProcessedId++;
doActualQuery(ids[idToProcess], function(result) {
processedIds[idToProcess] = result;
processedIdCount++;
if (processedIdCount === ids.length) {
nextTick(callback, processedIds);
}
});
}
if (intervalId && lastProcessedId === ids.length)
clearInterval(intervalId);
}
intervalId = setInterval(queryApi, 1000/6);
}
queryAllIds(ids, function (processedIds) {
// Do stuff here
});
We ended up using rate limiter which provided the rate limiting we needed right out of the box. https://www.npmjs.com/package/limiter

Youtube onYouTubePlayerReady() function not firing

This question has been asked many times before, however I couldn't solve it even after referring the solutions.
I have an embedded Youtube Player. I am following
https://developers.google.com/youtube/js_api_reference#Examples.
Below is my JS code:
var params = {
scale:'noScale',
salign:'lt',
menu:'false',
allowfullscreen :'true',
wmode :'transparent',
allowScriptAccess: 'always'
};
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube.com/v/vPxGBYJ9Wt8?enablejsapi=1&playerapiid=ytplayer&version=3", "ytapiplayer", "560", "315", "8.0.0", null, null, params, atts);
function onYouTubePlayerReady(playerId) {
alert("youtube player ready");
ytplayer = document.getElementById("myytplayer");
ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}
var count = 0;
function onytplayerStateChange(newState) {
alert("inside");
if (newState == 1){
count = count + 1;
//Trying to count the number of times video is played
}
}
I can watch the video, but I am not able to get any of the alert messages.
It is being served from a Web Server. Also, the JS code is written within a JSP page. What are the things I should check?
Update
I tried the same code in a separate fie and it works just fine.
I solved the problem by putting the window.onYouTubePlayerReady inside $(document).ready(function())

Persist data on disk using chrome extension API

I am trying to save some data which should be available even when restart the browser So this data should persist. I am using Chrome Storage Sync API for this. But when I am restarting my browser, I get empty object on using chrome.storage.get.
Here is my sample code:
SW.methods.saveTaskListStore = function() {
chrome.storage.sync.set({
'taskListStore': SW.stores.taskListStore
}, function() {
if (SW.callbacks.watchProcessSuccessCallback) {
SW.callbacks.watchProcessSuccessCallback(SW.messages.INFO_DATA_SAVED);
SW.callbacks.watchProcessSuccessCallback = null;
}
});
};
SW.methods.loadTaskListStore = function() {
SW.stores.loadTaskListStore = [];
chrome.storage.sync.get('taskListStore', function(taskFeed) {
var tasks = taskFeed.tasks;
if (tasks && !tasks.length) {
SW.stores.loadTaskListStore = tasks;
}
});
};
I guess I am using the Wrong API.
If this is not some copy-paste error, you are storing under key taskListStore and trying to get data under key loadTaskListStore.
Besides that, according to the documentation on StorageArea.get(), the result object is an object with items in their key-value mappings. Thus, in your case, you should do:
chrome.storage.sync.get("taskListStore", function(items) {
if (items.taskListStore) {
var tasks = items.taskListStore.tasks;
...

Moving from localStorage to chrome.storage

I would like to use chrome.storage API to save the settings of my users instead of localStorage in my Chrome extension.
Currently my options.js (with localStorage and JSON) file looks like this:
$(function(){ //jQuery Ready
// INIT
$("#notifysav").hide();
// STORAGE
if(localStorage['options']){
var o = JSON.parse(localStorage['options']);
$("#option1").attr('checked', o.option1);
$("#option2").attr('checked', o.option2);
.... [list of options]
}
// Save Button Click event
$("#save").live('click', function(){
localStorage['options'] = JSON.stringify({
"option1":$("#option1").attr('checked'),
"option2":$("#option2").attr('checked'),
.... [list of options]
});
// notification
$("#notifysav").fadeIn("slow").delay(2000).fadeOut("slow");
// reload to apply changes
chrome.extension.sendRequest({action: 'reload'});
});
});// #jQuery ready
My question is how to convert my current code to use the chrome.storage API. From what I understand, I would apply those changes:
$(function(){
// INIT
var storage = chrome.storage.sync;
$("#notifysav").hide();
// Load Options
loadOptions();
// Save Button Click Event
$("#save").live('click',function(){ saveOptions(); });
function loadOptions() {
storage.get( /* Something */ )
}
function saveOptions() {
var option1 = $("#option1").attr('checked');
var option2 = $("#option2").attr('checked');
storage.set({"option1":option1,"option2":option2}, function() {
// Notification
$("#notifysav").fadeIn("slow").delay(2000).fadeOut("slow");
// Reload Event to apply changes
chrome.extension.sendRequest({action: 'reload'});
});
}
});
Thanks for your help!
If I understand correctly, your main problem is how to retrieve data from the storage. Here is what can be done:
chrome.storage.local.get(null, function(all){console.log(all)});
will return you an object with all keys and values stored in the storage, in your case it will output:
Object {option1: "value1", option2: "value2"}
Also you can get just one of the keys:
chrome.storage.local.get("optionkey", function(all){console.log(all)});
or an array of keys:
chrome.storage.local.get(["opt1", "opt2"], function(all){console.log(all)});
In any case you can access data in the callback just by key names.
Thanks for your reply. I finally managed to get something working by changing my original code as follows:
$(function(){
// INIT
const storage = chrome.storage.sync;
var options = new Array();
$("#notifysav").hide();
loadOptions();
// STORAGE
// Save Button Click event
$("#save").live('click', function(){ saveOptions(); });
function loadOptions() {
storage.get('options', function(o) {
$("#option1").attr('checked', o.options[0]);
$("#option2").attr('checked', o.options[1]);
...
});
}
function saveOptions() {
options[0] = $("#option1").prop('checked');
options[1] = $("#option2").prop('checked');
...
//console.log(options);
storage.set({'options':options},function(){
$("#notifysav").fadeIn("slow").delay(2000).fadeOut("slow");
chrome.extension.sendRequest({action: 'reload'});
});
}
});

Resources