-
-
Notifications
You must be signed in to change notification settings - Fork 37
Added MediaSession support (notification controls on mobile) #187
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
| /** | ||
| * @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", { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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