gpt4 book ai didi

javascript - 通过 spotify web api 和 javascript 播放歌曲时出现问题

转载 作者:行者123 更新时间:2023-11-29 20:48:59 25 4
gpt4 key购买 nike

我正在构建一个与 spotify 交互的基于网络的应用程序。我从 C# 开始,访问 API、拉出我的播放列表并从中拉出轨道没有问题,但似乎您无法使用位于此处的 spotify Web API 播放歌曲:

https://developer.spotify.com/documentation/web-api/

然后我开始查看位于此处的 Web Playback API:

https://developer.spotify.com/documentation/web-playback-sdk/

我打算用 c# 编写大部分内容,因为我的 c# 比我的 javascript 强大得多。 c# 部分正在运行。我可以获得授权 token ,提取我的播放列表和轨道。我打算将此信息传递给 javascript。

我从 spotify 开发者页面中提取了以下 javascript。我只是有点理解它,所以我不知道为什么它不起作用。非常感谢您提供的任何帮助。

<script src="https://sdk.scdn.co/spotify-player.js"></script>

<script>

window.onSpotifyWebPlaybackSDKReady = () => {
// You can now initialize Spotify.Player and use the SDK
};

const play = ({
spotify_uri,
playerInstance: {
_options: {
getOAuthToken,
id
}
}
}) => {
getOAuthToken(access_token => {
fetch('https://api.spotify.com/v1/me/player/play', {
method: 'PUT',
body: JSON.stringify({ uris: [spotify_uri] }),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ${myaccesstoken}'
},
});
});
};

play({
playerInstance: new Spotify.Player({ name: "..." }),
spotify_uri: 'spotify:track:7xGfFoTpQ2E7fRF5lN10tr',
});

</script>

最佳答案

tl;dr:此答案底部的工作片段!


你这样做

play({
playerInstance: new Spotify.Player({ name: "..." }),
spotify_uri: 'spotify:track:7xGfFoTpQ2E7fRF5lN10tr',
});

以下之外。

window.onSpotifyWebPlaybackSDKReady = () => {
// You can now initialize Spotify.Player and use the SDK
};

这意味着 play无需等待 Spotify Web Playback SDK 加载即可立即调用。正如评论所说Spotify.Player可以尽快使用 onSpotifyWebPlaybackSDKReady被调用了。


另一个问题是您实际上从未创建过 Spotify Connect 设备。这是使用 Spotify Web API 来控制该设备所必需的。这可以通过调用 connect 来实现在 Spotify.Player 上实例。为了知道什么时候connect完成后,播放器就可以播放歌曲了,您需要先定义一个监听器,如下所示。

player.addListener('ready', ({ device_id }) => {
console.log('Ready with Device ID', device_id);
});

所以您实际上需要两个不同的 Spotify API 才能实现您的目标。首先,您需要 Spotify Web Playback SDK 才能创建 Spotify Connect 设备(Spotify 文档将其称为播放器)。之后,您可以使用 Spotify 的 Web API 控制这个确切的 Spotify Connect 设备。


以下片段将播放歌曲。

警告:这将在没有任何控制元素的情况下在您的浏览器中播放音乐!

此代码段需要一个可以获取的访问 token here单击绿色按钮 Get Your Web Playback SDK Access Token .然后需要将 token 复制粘贴到替换 <YOUR_ACCESS_TOKEN_HERE> 的代码段的第 11 行中.

index.html

<!-- Load the Spotify Web Playback SDK -->
<script src="https://sdk.scdn.co/spotify-player.js"></script>

<script>
// Called when the Spotify Web Playback SDK is ready to use
window.onSpotifyWebPlaybackSDKReady = () => {

// Define the Spotify Connect device, getOAuthToken has an actual token
// hardcoded for the sake of simplicity
var player = new Spotify.Player({
name: 'A Spotify Web SDK Player',
getOAuthToken: callback => {
callback('<YOUR_ACCESS_TOKEN_HERE>');
},
volume: 0.1
});

// Called when connected to the player created beforehand successfully
player.addListener('ready', ({ device_id }) => {
console.log('Ready with Device ID', device_id);

const play = ({
spotify_uri,
playerInstance: {
_options: {
getOAuthToken,
id
}
}
}) => {
getOAuthToken(access_token => {
fetch(`https://api.spotify.com/v1/me/player/play?device_id=${id}`, {
method: 'PUT',
body: JSON.stringify({ uris: [spotify_uri] }),
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${access_token}`
},
});
});
};

play({
playerInstance: player,
spotify_uri: 'spotify:track:7xGfFoTpQ2E7fRF5lN10tr',
});
});

// Connect to the player created beforehand, this is equivalent to
// creating a new device which will be visible for Spotify Connect
player.connect();
};
</script>

关于javascript - 通过 spotify web api 和 javascript 播放歌曲时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52880305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com