Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add mediasession support #8309

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
always need to wait for playlist
  • Loading branch information
mister-ben committed Jun 3, 2023
commit 2a10d09b934af0f4d82c61659676c6c9484ed345
17 changes: 6 additions & 11 deletions src/js/mediasession.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const initMediaSession = function() {
}]
];

// Using Googles' recommendation that expects some handler may not be settable, especially as we
// Using Google's recommendation that expects some handler may not be settable, especially as we
// want to support older Chrome
// https://web.dev/media-session/#let-users-control-whats-playing
for (const [action, handler] of actionHandlers) {
Expand All @@ -69,11 +69,7 @@ export const initMediaSession = function() {
};

// Only setup playlist handlers if / when playlist plugin is present
if (this.usingPlugin('playlist')) {
setUpMediaSessionPlaylist();
} else {
this.on('pluginsetup:playlist', setUpMediaSessionPlaylist);
}
this.on('pluginsetup:playlist', setUpMediaSessionPlaylist);

/**
*
Expand Down Expand Up @@ -111,7 +107,7 @@ export const initMediaSession = function() {
const updatePositionState = () => {
const dur = parseFloat(this.duration());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, is there a case that requires a parseFloat?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That can probably go

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a specification perspective (w3c, whatwg), it seems safe to remove the parseFloat, especially as the condition immediately following it prevents any NaN or Infinity. However, if you prefer to keep it, that's fine too.


if (Number.isFinite(dur) && parseFloat(dur)) {
if (Number.isFinite(dur)) {
ms.setPositionState({
duration: dur,
playbackRate: this.playbackRate(),
Expand All @@ -125,12 +121,11 @@ export const initMediaSession = function() {
ms.playbackState = 'playing';
});

this.on(['paused', 'paused'], () => {
updateMediaSession();
ms.playbackState = 'playing';
this.on('paused', () => {
ms.playbackState = 'paused';
});

if ('setPositionState' in ms) {
this.on(['playing', 'seeked', 'ratechange'], updatePositionState);
this.on('timeupdate', updatePositionState);
}
};
File renamed without changes.