使用Python播放,暂停等Spotify Web播放SDK

时间:2018-08-15 19:18:39

标签: python flask spotify

我正在使用Spotify Web播放SDK创建一个可以播放我的Spotify帐户中的音乐的网页。我正在Flask中使用HTML模板来运行此代码...

<!DOCTYPE html>
<html>
<head>
  <title>Spotify Web Playback SDK Quick Start Tutorial</title>
</head>
<body>
  <h1>Spotify Web Playback SDK Quick Start Tutorial</h1>
  <h2>Open your console log: <code>View > Developer > JavaScript Console</code></h2>

  <h3>PAUSE</h3>
  <script src="https://sdk.scdn.co/spotify-player.js"></script>
  <script>
    window.onSpotifyWebPlaybackSDKReady = () => {
        const token = 'BQB93YTT8NKaM-Xhur02dkUUnacErZCZ8usjI-Wqw9E1Z3K2hNckoeQttPOk8ZskHpVCKaH6aBN86v2fBoUFsV_9-DFM0_61yZgBPtXySqXtc7h66dxXZJCLnxHRUBStT9wx2ppGfFK90GMXYrm6hUUt3dwzom4LMbgR';
        const player = new Spotify.Player({
            name: 'Web Playback SDK Quick Start Player',
            getOAuthToken: cb => { cb(token); }
        });

        // Error handling
        player.addListener('initialization_error', ({ message }) => { console.error(message); });
        player.addListener('authentication_error', ({ message }) => { console.error(message); });
        player.addListener('account_error', ({ message }) => { console.error(message); });
        player.addListener('playback_error', ({ message }) => { console.error(message); });

        // Playback status updates
        player.addListener('player_state_changed', state => { console.log(state); });

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

        // Not Ready
        player.addListener('not_ready', ({ device_id }) => {
            console.log('Device ID has gone offline', device_id);
        });

        // Connect to the player!
        player.connect();

        window.onclick = myFunction;

        // Pause or Play current song
        function myFunction() {
            player.togglePlay()
        }
    };
  </script>
</body>
</html>

我希望能够从python发送一些代码到网页上,从而触发播放或暂停功能。

我已经尝试过使用flask来发布方法和其他东西,但是它们都需要页面重新加载新代码。我需要一种可以在页面加载后动态使用的解决方案。

0 个答案:

没有答案