2013-10-04 2 views
0

Я хотел бы знать, есть ли способ узнать, какой плейлист в данный момент воспроизводится на месте. Я прочитал API и нашел функцию isLoaded, которая возвращает логическое значение. Я мог бы пройти через все плейлисты и получить, какой из них загружен, но я хотел бы знать, есть ли способ сделать это более непосредственно.Текущий плейлист на Spotify?

ответ

2

Вы можете узнать uri в контексте того, что играет в проигрывателе с помощью:

require([ 
    '$api/models', 
], function(models) { 
    'use strict'; 

    // find out initial status of the player 
    models.player.load(['context']).done(function(player) { 
    // player.context.uri will contain the uri of the context 
    }); 

    // subscribe to changes 
    models.player.addEventListener('change', function(m) { 
    // m.data.context.uri will contain the uri of the context 
    }); 
}); 

Вы можете использовать это uri для извлечения свойств, таких как name. В следующем примере мы получаем имя списка воспроизведения, если контекст текущей дорожки является списком воспроизведения:

require([ 
    '$api/models', 
], function(models) { 
    'use strict'; 

    /** 
    * Returns whether the uri belongs to a playlist. 
    * @param {string} uri The Spotify URI. 
    * @return {boolean} True if the uri belongs to a playlist, false otherwise. 
    */ 
    function isPlaylist(uri) { 
    return models.fromURI(uri) instanceof models.Playlist; 
    } 

    /** 
    * Returns the name of the playlist. 
    * @param {string} playlistUri The Spotify URI of the playlist. 
    * @param {function(string)} callback The callback function. 
    */ 
    function getPlaylistName(playlistUri, callback) { 
    models.Playlist.fromURI(models.player.context.uri) 
     .load('name') 
     .done(function(playlist){ 
     callback(playlist.name); 
    }); 
    } 

    // find out initial status of the player 
    models.player.load(['context']).done(function(player) { 
    var uri = player.context.uri; 
    if (isPlaylist(uri)) { 
     getPlaylistName(player.context.uri, function(name) { 
     // do something with 'name' 
     }); 
    } 
    }); 

    // subscribe to changes 
    models.player.addEventListener('change', function(m) { 
    var uri = m.data.context.uri; 
    if (isPlaylist(uri)) { 
     getPlaylistName(m.data.context.uri, function(name) { 
     // do something with 'name' 
     }); 
    } 
    }); 
}); 
Смежные вопросы