Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
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
Binary file added js/webui/src/5-seconds-of-silence.mp3
Binary file not shown.
3 changes: 3 additions & 0 deletions js/webui/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
<div id="app-container"></div>
<div id="notification-container"></div>
</body>
<audio id="silence" loop>
<source src="/5-seconds-of-silence.mp3" type="audio/mp3" />
</audio>
</html>
3 changes: 3 additions & 0 deletions js/webui/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import urls, { getPathFromUrl } from './urls'
import { playlistTableKey } from './playlist_content';
import { PlaybackState } from 'beefweb-client/src';
import { SettingsView, View } from './navigation_model';
import MediaSessionController from './mediasession_controller';

const client = new PlayerClient(new RequestHandler());
const settingsStore = new SettingsStore();
Expand All @@ -34,6 +35,7 @@ const touchModeController = new TouchModeController(settingsModel);
const cssSettingsController = new CssSettingsController(settingsModel);
const windowController = new WindowController(playerModel);
const router = new Navigo(null, true);
const mediaSessionController = new MediaSessionController(client, playerModel);

router.on({
'/': () => {
Expand Down Expand Up @@ -135,6 +137,7 @@ touchModeController.start();
cssSettingsController.start();
appModel.start();
windowController.start();
mediaSessionController.start();
router.resolve();

const appComponent = (
Expand Down
78 changes: 78 additions & 0 deletions js/webui/src/mediasession_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import DataSource from "./data_source";
import PlayerModel from "./player_model";
import silenceMp3 from "./5-seconds-of-silence.mp3";

export default class MediaSessionController {
Copy link
Owner

Choose a reason for hiding this comment

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

Formatting does not match other files

Copy link
Author

Choose a reason for hiding this comment

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

Changed to single quotes and tab width 4

/**
* @param {any} client
* @param {PlayerModel} playerModel
*/
constructor(client, playerModel) {
this.dataSource = new DataSource(client);
this.playerModel = playerModel;
}

start() {
if (!"mediaSession" in navigator) {
return;
}

navigator.mediaSession.setActionHandler("play", () =>
this.playerModel.play()
);
navigator.mediaSession.setActionHandler("pause", () =>
this.playerModel.pause()
);
navigator.mediaSession.setActionHandler("previoustrack", () =>
this.playerModel.previous()
);
navigator.mediaSession.setActionHandler("nexttrack", () =>
this.playerModel.next()
);

this.dataSource.on("player", (player) => this.updateMetadata(player));
this.dataSource.watch("player", {
Copy link
Owner

@hyperblast hyperblast Oct 4, 2020

Choose a reason for hiding this comment

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

Only one type can watch on data source, this logic should be in PlayerModel instead.

Copy link
Author

Choose a reason for hiding this comment

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

Moved to PlayerModel, but it should probably be refactored later because the activeItem's columns are becoming messy

trcolumns: ["%artist%", "%album%", "%title%"],
});

this.playerModel.on("change", () => this.updatePlaybackState());

this.updatePlaybackState();
this.dataSource.start();
}

updatePlaybackState() {
const playbackStateMap = {
playing: "playing",
paused: "paused",
stopped: "none",
};

const audioElement = document.getElementById("silence");

switch (this.playerModel.playbackState) {
case "playing":
audioElement.play();
break;
case "paused":
audioElement.pause();
break;
default:
break;
}

navigator.mediaSession.playbackState =
playbackStateMap[this.playerModel.playbackState];
}

updateMetadata(player) {
const { activeItem } = player;
const [artist, album, title] = activeItem.columns;

navigator.mediaSession.metadata = new MediaMetadata({
artist,
album,
title,
});
}
}
14 changes: 13 additions & 1 deletion js/webui/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ function configCommon(config, params)
});

config.module.rules.push({
test: /(\.svg|\.png)$/,
test: /(\.svg|\.png|\.mp3)$/,
loader: 'url-loader',
options: {
name: '[name].[ext]',
limit: 1024
}
});

config.optimization = {
Copy link
Owner

Choose a reason for hiding this comment

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

This looks unrelated to this PR, besides that I'm not sure if there is a benefit of having vendors chunk.

Copy link
Author

Choose a reason for hiding this comment

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

It's out of scope of this PR, but the added code pushed the bundle over 300KB, breaking the build

Copy link
Owner

Choose a reason for hiding this comment

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

The limit is raised in other PR, probably you can do the same.

Copy link
Author

Choose a reason for hiding this comment

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

Removed code splitting and raised bundle limit

splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
}
}
}
}
}

function configApp(config, params)
Expand Down