使用Spotify API搜索专辑添加专辑名称和艺术家名称

时间:2014-10-28 15:52:13

标签: javascript handlebars.js spotify

使用快速项目在Spotify中搜索音乐曲目...您可以将曲目名称添加到文本区域,并生成一个匹配的曲目ID列表,其中包含艺术家姓名和专辑名称+艺术品...下面是我目前处理的代码示例...坚持获取专辑名称/专辑图像和艺术家姓名...

示例 - 我使用的是: 追踪:我所有人 结果应该是艺术家:约翰传奇 轨道ID是否正确:3U4isOIWM3VvDubwSI3y7a

显示为第一个....

http://jsfiddle.net/61f64ccq/2/

另外(更新此位......)

http://jsfiddle.net/UT7bQ/212/     {{name}} - {{this.artists.name}} 尝试为Handlebar.js添加代码,以便从.JSON数据中获取艺术家名称......

下面是第一个例子......

使用纯JS / Handlebar.js和HTML5 ....任何帮助都会很棒...尝试{{artists.name}}但没有发生任何事情......

<div class="container">
<h1>Search for a Track - Spotify</h1>
<form id="search-form">
<input type="text" id="query" value="" class="form-control" />
<input type="submit" id="search" class="btn btn-primary" value="Search" />
</form>
<div id="results"></div>
</div>
<script id="results-template" type="text/x-handlebars-template">
{{#each tracks.items}}
<div >{{id}} -         
<br /> {{name}} <br/></div>
// Issue - Image
<div style="border:1px solid red; background-image:url({{images.2.url}})" data-album-id="{{id}}" class="cover"></div>
// Issue - Artist Name and Album Name
<div style="border:1px solid green; "> Artist name = {  } -  Album name = { } </div>

部分Javascript代码 -

<script>
var templateSource = document.getElementById('results-template').innerHTML,
template = Handlebars.compile(templateSource),
resultsPlaceholder = document.getElementById('results'),
playingCssClass = 'playing',
audioObject = null;
var fetchTracks = function (albumId, callback) {
$.ajax({
url: 'https://api.spotify.com/v1/albums/' + albumId,
success: function (response) {
callback(response);
}
});
};
var searchAlbums = function (query) {
$.ajax({
url: 'https://api.spotify.com/v1/search',
data: {
q: query,
type: 'track'
// Gets the track - search query
},
success: function (response) {
resultsPlaceholder.innerHTML = template(response);
}
});
};
// This was for playing the Track in your window...  Not working in the Track format...
results.addEventListener('click', function (e) {
var target = e.target;
if (target !== null && target.classList.contains('cover')) {
if (target.classList.contains(playingCssClass)) {
audioObject.pause();
} else {
if (audioObject) {
audioObject.pause();
}
fetchTracks(target.getAttribute('data-album-id'), function (data) {
audioObject = new Audio(data.tracks.items[0].preview_url);
audioObject.play();
target.classList.add(playingCssClass);
audioObject.addEventListener('ended', function () {
target.classList.remove(playingCssClass);
});
audioObject.addEventListener('pause', function () {
target.classList.remove(playingCssClass);
});
});
}
}
});
document.getElementById('search-form').addEventListener('submit', function (e) {
e.preventDefault();
searchAlbums(document.getElementById('query').value);
}, false);
</script>

由于 西蒙

1 个答案:

答案 0 :(得分:0)

以下是我改变并添加了一个小提琴示例 - 以Handlebars为例,以正确的格式获取专辑信息 - 图像/艺术家,专辑类型和专辑名称....希望对其他人有用....

http://jsfiddle.net/61f64ccq/4/

<script id="results-template" type="text/x-handlebars-template">
{{#each tracks.items}}
<div >{{id}} -         
<br /> {{name}} <br/></div>
<div style="border:1px solid red; background-image:url({{album.images.2.url}})" data-album-id="{{id}}" class="cover"></div>
<div style="border:1px solid green; "> Artist name = {{artists.0.name}} </div>
<div style="border:1px solid green; "> Album Type = {{album.album_type}} </div>
<div style="border:1px solid green; "> Album name = {{album.name}} </div>
{{/each}}
</script>

感谢每个人都在看这个 - 现在已经修好了...干杯西蒙