Skip to content

Commit eb0898f

Browse files
committed
Add getPlayerMediaStatus instance method
1 parent 3feae65 commit eb0898f

File tree

7 files changed

+90
-12
lines changed

7 files changed

+90
-12
lines changed

packages/webamp-docs/docs/06_API/03_instance-methods.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ Get the current "playing" status. The return value is one of: `"PLAYING"`, `"STO
104104
const isPlaying = webamp.getMediaStatus() === "PLAYING";
105105
```
106106

107+
### `getPlayerMediaStatus(): PlayerMediaStatus`
108+
109+
Get the current "playing" status of the player. Similar to `getMediaStatus()`, but can differentiate between different reasons why the player might not be playing, such as "ENDED" when the end of the playlist has been reached or "CLOSED" when the player has been closed.
110+
111+
The return value is one of: `"PLAYING"`, `"STOPPED"`, `"PAUSED"`, `"ENDED"`, or `"CLOSED"`.
112+
113+
**Since** 2.1.3
114+
115+
```ts
116+
const playerStatus = webamp.getPlayerMediaStatus();
117+
if (playerStatus === "ENDED") {
118+
console.log("Playlist has ended");
119+
} else if (playerStatus === "CLOSED") {
120+
console.log("Player is closed");
121+
}
122+
```
123+
107124
### `pause(): void`
108125

109126
Pause the current track.

packages/webamp/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Added new `Webamp` instance methods:
1010
- `webamp.toggleShuffle`
1111
- `webamp.toggleRepeat`
12+
- `webamp.getPlayerMediaStatus`
1213
- Add new config option `enableMediaSession` to allow Webamp to connect to the browser's Media Session API. This enables OS/hardware level media controls like play/pause/next/previous.
1314
- Ensure the promise returned from `renderWhenReady` only resolves after the Webamp instance has been fully mounted and inserted into the DOM. Previously it resolved after the DOM node was created but before it was inserted into the DOM.
1415

packages/webamp/js/constants.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
LoadStyle,
66
TimeMode,
77
WindowId,
8+
PlayerMediaStatus,
89
} from "./types";
910
import baseSkin from "./baseSkin.json";
1011
export const BANDS: Band[] = [
@@ -64,9 +65,17 @@ export const TIME_MODE: Record<TimeMode, TimeMode> = {
6465
REMAINING: "REMAINING",
6566
};
6667

67-
// TODO: Convert to enum once we are fully Typescript
6868
export const MEDIA_STATUS: Record<MediaStatus, MediaStatus> = {
6969
PLAYING: "PLAYING",
7070
STOPPED: "STOPPED",
7171
PAUSED: "PAUSED",
7272
};
73+
74+
export const PLAYER_MEDIA_STATUS: Record<PlayerMediaStatus, PlayerMediaStatus> =
75+
{
76+
PLAYING: "PLAYING",
77+
STOPPED: "STOPPED",
78+
PAUSED: "PAUSED",
79+
ENDED: "ENDED",
80+
CLOSED: "CLOSED",
81+
};

packages/webamp/js/reducers/media.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Action, MediaStatus, TimeMode } from "../types";
1+
import { Action, PlayerMediaStatus, TimeMode } from "../types";
22
import {
33
PLAY,
44
STOP,
@@ -13,8 +13,10 @@ import {
1313
TOGGLE_TIME_MODE,
1414
UPDATE_TIME_ELAPSED,
1515
LOAD_SERIALIZED_STATE,
16+
CLOSE_WINAMP,
17+
OPEN_WINAMP,
1618
} from "../actionTypes";
17-
import { TIME_MODE, MEDIA_STATUS } from "../constants";
19+
import { TIME_MODE, PLAYER_MEDIA_STATUS } from "../constants";
1820
import { MediaSerializedStateV1 } from "../serializedStates/v1Types";
1921

2022
export interface MediaState {
@@ -24,7 +26,7 @@ export interface MediaState {
2426
balance: number;
2527
shuffle: boolean;
2628
repeat: boolean;
27-
status: MediaStatus;
29+
status: PlayerMediaStatus;
2830
}
2931

3032
const defaultState = {
@@ -39,7 +41,7 @@ const defaultState = {
3941
shuffle: false,
4042
repeat: false,
4143
// TODO: Enforce possible values
42-
status: MEDIA_STATUS.STOPPED,
44+
status: PLAYER_MEDIA_STATUS.STOPPED,
4345
};
4446

4547
const media = (
@@ -50,12 +52,17 @@ const media = (
5052
// TODO: Make these constants
5153
case PLAY:
5254
case IS_PLAYING:
53-
return { ...state, status: MEDIA_STATUS.PLAYING };
55+
return { ...state, status: PLAYER_MEDIA_STATUS.PLAYING };
5456
case PAUSE:
55-
return { ...state, status: MEDIA_STATUS.PAUSED };
57+
return { ...state, status: PLAYER_MEDIA_STATUS.PAUSED };
5658
case STOP:
59+
return { ...state, status: PLAYER_MEDIA_STATUS.STOPPED };
5760
case IS_STOPPED:
58-
return { ...state, status: MEDIA_STATUS.STOPPED };
61+
return { ...state, status: PLAYER_MEDIA_STATUS.ENDED };
62+
case OPEN_WINAMP:
63+
return { ...state, status: PLAYER_MEDIA_STATUS.STOPPED };
64+
case CLOSE_WINAMP:
65+
return { ...state, status: PLAYER_MEDIA_STATUS.CLOSED };
5966
case TOGGLE_TIME_MODE:
6067
const newMode =
6168
state.timeMode === TIME_MODE.REMAINING

packages/webamp/js/selectors.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import {
88
WindowPositions,
99
PlaylistStyle,
1010
TransitionType,
11-
MediaStatus,
1211
TimeMode,
1312
SkinImages,
1413
Cursors,
1514
SkinRegion,
1615
GenLetterWidths,
1716
MilkdropMessage,
1817
DummyVizData,
18+
PlayerMediaStatus,
19+
MediaStatus,
1920
} from "./types";
2021
import { createSelector, defaultMemoize } from "reselect";
2122
import * as Utils from "./utils";
@@ -29,6 +30,7 @@ import {
2930
MEDIA_TAG_REQUEST_STATUS,
3031
WINDOWS,
3132
VISUALIZERS,
33+
PLAYER_MEDIA_STATUS,
3234
} from "./constants";
3335
import { createPlaylistURL } from "./playlistHtml";
3436
import * as fromTracks from "./reducers/tracks";
@@ -337,11 +339,28 @@ export const getCurrentTrackDisplayName = createSelector(
337339
return getName(id);
338340
}
339341
);
340-
341-
export const getMediaStatus = (state: AppState): MediaStatus => {
342+
export const getPlayerMediaStatus = (state: AppState): PlayerMediaStatus => {
342343
return state.media.status;
343344
};
344345

346+
export const getMediaStatus = createSelector(
347+
getPlayerMediaStatus,
348+
(status: PlayerMediaStatus): MediaStatus => {
349+
switch (status) {
350+
case "PLAYING":
351+
case "PAUSED":
352+
return status;
353+
case "STOPPED":
354+
case "ENDED":
355+
case "CLOSED":
356+
return "STOPPED";
357+
default:
358+
const s: never = status;
359+
throw new Error(`Unknown media status: ${s}`);
360+
}
361+
}
362+
);
363+
345364
export const getMediaIsPlaying = (state: AppState) =>
346365
state.media.status === MEDIA_STATUS.PLAYING;
347366

packages/webamp/js/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,22 @@ export type MediaTagRequestStatus =
564564
| "COMPLETE"
565565
| "NOT_REQUESTED";
566566

567+
/** The status of the current media. */
567568
export type MediaStatus = "PLAYING" | "STOPPED" | "PAUSED";
568569

570+
/**
571+
* The media status of the player. Similar to MediaStatus but can discriminate
572+
* between different reasons for being stopped.
573+
*/
574+
export type PlayerMediaStatus =
575+
| "PLAYING"
576+
| "STOPPED"
577+
| "PAUSED"
578+
/** We have reached the end of the playlist. */
579+
| "ENDED"
580+
/** The player is closed. */
581+
| "CLOSED";
582+
569583
export type LoadStyle = "BUFFER" | "PLAY" | "NONE";
570584

571585
export type TimeMode = "ELAPSED" | "REMAINING";

packages/webamp/js/webampLazy.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
Options,
1414
MediaStatus,
1515
PlaylistTrack,
16+
PlayerMediaStatus,
1617
} from "./types";
1718
import getStore from "./store";
1819
import App from "./components/App";
@@ -337,10 +338,20 @@ class Webamp {
337338
/**
338339
* Get the current "playing" status.
339340
*/
340-
getMediaStatus(): MediaStatus | null {
341+
getMediaStatus(): MediaStatus {
341342
return Selectors.getMediaStatus(this.store.getState());
342343
}
343344

345+
/**
346+
* Get the current "playing" status of the player. Similar to
347+
* `getMediaStatus()`, but can differentiate between different reasons why the
348+
* player might not be playing, such as "ENDED" when the end of the playlist
349+
* has been reached or "CLOSED" when the player has been closed.
350+
*/
351+
getPlayerMediaStatus(): PlayerMediaStatus {
352+
return Selectors.getPlayerMediaStatus(this.store.getState());
353+
}
354+
344355
/**
345356
* A callback which will be called when Webamp is _about to_ close. Returns an
346357
* "unsubscribe" function. The callback will be passed a `cancel` function

0 commit comments

Comments
 (0)