Spotify API:获取随机歌曲 [英] Spotify API: get random songs

查看:72
本文介绍了Spotify API:获取随机歌曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,stackoverflow 上的人们,

Hello people on stackoverflow,

我必须使用 Spotify 制作一个网络应用程序以供我学习.我决定制作一个应用程序,您可以在其中预览 30 秒随机歌曲.您可以喜欢或不喜欢这首歌.

I have to make a webapp with spotify for my study. I decided to make an application where you get a 30 seconds preview of a random song. You are able to like or dislike the song.

那么我该怎么做才能从 Spotify 中获取随机歌曲.任何人都可以请帮助我.这对我很有帮助!

So what do i have to do to get random songs out of spotify. Can anyone help me please. it would help me a lot!

感谢您的帮助!!

问候

编辑

我正在尝试使用 Spotify API 创建一个应用程序,该应用程序从 Spotify 库中查找并播放随机歌曲.

I'm trying to use the Spotify API to create an application that finds and plays a random song from the Spotify library.

在 Spotify API 中,您可以获得一首单曲.要获取曲目,您需要一个 TrackId.您可以从用户库中获取所有歌曲,但不是只从用户库中获取曲目,而是希望从整个 Spotify 库中获取曲目.在 Spotify API 开发人员区域,没有关于从 Spotify 库中获取所有曲目的内容.

In the Spotify API you can get one single track. To get the track, you need a TrackId. You can get all songs from user's library, but instead of getting just the tracks from the user's library, I want tracks from the whole spotify library. In the Spotify API Developers area there isn't something about getting all tracks from the spotify library.

我可以加载一张专辑并从该专辑中提取一首曲目.并且每次都这样做,但这一点都不容易.所以我想知道是否有更简单的解决方案来从 spotify 获取所有曲目.

I can load an album and get one track out of that album. And do that every single time, but that isn't easy at all. So I would like to know if there is an easier solution to get all tracks from spotify.

推荐答案

我得到了这个解决方案.我认为这是从 Spotify 中获取随机曲目的最佳解决方案.让我知道你们对此有何看法.它不是完全随机的,但它是一种.:)

I got this as a solution. I think this is the best solution to get random tracks out of Spotify. Let me know what you guys think of this. It isn't completely random, but it's a kind of.:)

//make a variable with some search queries and put it in an array. (you can create more search queries.
$getRandomSongsArray = array('%25a%25', 'a%25', '%25e%25', 'e%25', '%25i%25', 'i%25', '%25o%25', 'o%25');

//This will get a random result out of the array above
$getRandomSongs = $getRandomSongsArray[array_rand($getRandomSongsArray)];

//This will get a random offset number between 1 and 1000. So you get a random track. (you can change the numbers btw)
$getRandomOffset = rand(1, 1000);

//This is the url that gets the results out of the Spotify API. You have to put in the variables you created above.
$url = "https://api.spotify.com/v1/search?query=$getRandomSongs&offset=$getRandomOffset&limit=1&type=track&market=NL";

这篇关于Spotify API:获取随机歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆