Spotify Web Playback SDKの React ラッパーライブラリです。
このライブラリを使用することで、 Spotify プレイヤーを宣言的に扱うことが可能になります。
Spotify Web Playback SDK は npm レジストリに公開されておらず、 CDN からダウンロードすることになります。ですので使用する際は script タグを追加する必要があります。それはあまりうれしくないでしょう。このラッパーライブラリを使用すれば、 html の head に script タグを挿入する必要はありません。
素の SDK は宣言的な API を備えていません。それゆえに、 React アプリで使用するときおそらく苦戦するでしょう。このライブラリを使用すれば、使い慣れたカスタムフックを用いて SDK が提供するデータを処理することができます。
npm install react-spotify-web-playback-sdk
最新の OAuth トークンが用意されていることを仮定します。トークンに必要なスコープは "streaming"
, "user-read-email"
, "user-read-private"
です。 こちらのページでもアクセストークンを取得することが可能です。
WebPlaybackSDK
コンポーネントはこのライブラリのルートコンポーネントです。このライブラリのすべてのカスタムフックは WebPlaybackSDK
でラップされている必要があります。
import { WebPlaybackSDK } from "react-spotify-web-playback-sdk";
const AUTH_TOKEN = "your token here!";
const MySpotifyPlayer: React.VFC = () => {
const getOAuthToken = useCallback(callback => callback(AUTH_TOKEN), []);
return (
<WebPlaybackSDK
deviceName="My awesome Spotify app"
getOAuthToken={getOAuthToken}
volume={0.5}>
{/* `TogglePlay` and `SongTitle` will be defined later. */}
<TogglePlay />
<SongTitle />
</WebPlaybackSDK>
);
};
props.deviceName
は デバイス名として Spotify の公式アプリに表示される値です。
props.getOAuthToken
にはアクセストークンを受け取る callback
関数を引数に取る関数を渡します。
props.volume
は 0 から 1 の間の値を渡します。指定されない場合、初期値 1 が設定されます。
Spotify プレイヤーのインスタンスを取得します。インスタンスメソッドを呼び出すことで音楽の再生や一時停止を実行することができます。
import { useSpotifyPlayer } from "react-spotify-web-playback-sdk";
const TogglePlay: React.VFC = () => {
const player = useSpotifyPlayer();
if (player === null) return null;
return <button onClick={() => player.togglePlay()}>toggle play</button>;
};
SDK のセットアップが完了していない間は null が返されるため、null チェックを行います。
ボタンをクリックすると、 player.togglePlay()
メソッドが実行されます。
再生に関する状態を取得します。例えばポーズしているかどうかや再生位置などがプロパティとして含まれます。
import { usePlaybackState } from "react-spotify-web-playback-sdk";
const SongTitle: React.VFC = () => {
const playbackState = usePlaybackState();
if (playbackState === null) return null;
return <p>Current song: {playbackState.track_window.current_track.name}</p>;
};
Spotify との接続が確立されていない場合は null になるため、 null チェックを行います。
MySpotifyPlayer
を React アプリでレンダリングすると、Spotify モバイルアプリの Spotify Connect に My awesome Spotify app
が表示されるでしょう。そこで選択した上で、 toggle play
ボタンをクリックすれば再生が開始されます。
SDK をラップした React コンポーネントです。これは React-Naitive の環境では利用できません。
ライブラリに含まれるカスタムフックは、このコンポーネントの内側にないと動作しません。
prop name | type | default value | description |
---|---|---|---|
initialDeviceName |
string | (required) | Spotify Connect プレイヤーの名前の初期値。 |
getOAuthToken |
(callback: (token: string) => void) => void | (required) | player.connect() を呼び出すとき、またはユーザーのアクセストークンの有効期限が切れたときに呼び出される関数。 |
initialVolume |
number | 1 | ボリュームの初期値。0 から 1 の小数を指定します。 |
connectOnInitialized |
boolean | true | 初期化と同時に接続をするか。 |
const MyPlayer = () => {
const getOAuthToken = useCallback(callback => {
const token = fetchNewSpotifyToken();
callback(token);
}, []);
return (
<WebPlaybackSDK
deviceName="My Spotify App"
getOAuthToken={getOAuthToken}
volume={0.5}
connectOnInitialized={true}>
<SomeComponentsUsingCustomHook />
</WebPlaybackSDK>
);
};
SDK がダウンロードされて準備が完了したかどうかの真偽値を返すカスタムフックです。
function useWebPlaybackSDKReady(): boolean;
const MyPlayer = () => {
const webPlaybackSDKReady = useWebPlaybackSDKReady();
if (!webPlaybackSDKReady) return <div>Loading...</div>;
return <div>Spotify App is ready!</div>;
};
resume()
や seek(pos)
などの プレイヤーを操作する関数を備えたオブジェクトを返すカスタムフックです。
function useSpotifyPlayer(): Spotify.Player | null;
const PauseResumeButton = () => {
const player = useSpotifyPlayer();
if (player === null) return null;
return (
<div>
<button onClick={() => player.pause()}>pause</button>
<button onClick={() => player.resume()}>resume</button>
</div>
);
};
プレイヤーの再生状態を表すオブジェクトを取得するカスタムフックです。 オブジェクトが持つプロパティの詳細はSpotify for Developers のリファレンス を参照してください。
interval
が true
の場合、 durationMS
で指定した間隔で再生中にステート更新が発生します。 false
の場合でも、SpotifyPlayer.pause
や SpotifyPlayer.resume
によるステート更新は発生します。
function usePlaybackState(
interval?: boolean = false,
durationMS?: number = 1000,
): Spotify.PlaybackState | null;
const SongTitle: React.VFC = () => {
const playbackState = usePlaybackState();
if (playbackState === null) return null;
return <p>Current song: {playbackState.track_window.current_track.name}</p>;
};
プレイヤーデバイスの状態を表すオブジェクトを取得するカスタムフックです。 Spotify for Developers リファレンスに拠れば、インターネット接続がない場合 "not_ready"
になります。
device_id
は, 例えば Spotify Web API の再生 API (参考)のパラメーターに渡す値として使用できます。
type PlayerDevice = {
device_id: string;
status: "ready" | "not_ready";
};
function usePlayerDevice(): PlayerDevice | null;
const SPOTIFY_URI = "spotify:track:7xGfFoTpQ2E7fRF5lN10tr";
const PlayCarlyRaeJepsen = () => {
const device = usePlayerDevice();
const playCarlyRaeJepsen = () => {
if (device === null) return;
fetch(
`https://api.spotify.com/v1/me/player/play?device_id=${device.device_id}`,
{
method: "PUT",
body: JSON.stringify({ uris: [SPOTIFY_URI] }),
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${AUTH_TOKEN}`,
},
},
);
};
if (device === null) return null;
return <button onClick={playCarlyRaeJepsen}>Play Carly Rae Jepsen</button>;
};
エラーが発生した場合に message を含むオブジェクトが返されます。それ以外は null が返されます。
type ErrorState = {
message: string;
type:
| "account_error"
| "authentication_error"
| "initialization_error"
| "playback_error";
};
function useErrorState(): ErrorState | null;
const ErrorState = () => {
const errorState = useErrorState();
if (errorState === null) return null;
return <p>Error: {errorState.message}</p>;
};
MIT